


function debu(x) {
    $("#main_container").html($("#main_container").html() + "<span style='font-family:tahoma; font-size:7pt'>"+x+"</span>" + "&nbsp;");
}


function elapsedstring(n) {

    var sec = n;

    if (sec < 30) {
        return "a few moments";
    }

    if (sec < 3600) {

        min = Math.round(sec/60);

        return (min+" minute" + (min == 1 ? "" : "s"));

    }  


    if (sec < 86400) {
        hr = Math.round(sec/3600);
        mn = Math.round((sec%3600)/60);

        if (hr < 2) {
            return (hr + " hour" + (hr == 1 ? "" : "s") + " " + mn + " minutes");
        } else {
          
            return (hr + " hour" + (hr == 1 ? "" : "s"));
        }
        
    }

    hr = Math.round((sec%86400)/3600);
    return Math.round(sec/86400) + " days " + hr + " hours ";

}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function strlcs(S1, S2) {

    Start = 0;
    Max = 0;

    var i;
    var j;
    var x;

    for (i = 0; i < S1.length; i++) {
        for (j = 0; j < S2.length; j++) {
            x = 0;
            while (S1.charAt(i + x) == S2.charAt(j + x)) {
                x++;
                if (((i + x) >= S1.length) || ((j + x) >= S2.length)) break;
            }
            if (x > Max) {
                Max = x;
                Start = i;
            }
         }
    }
    return S1.substring(Start, (Start + Max));
}


function strnew(a, b) {
    return($.trim(a).toLowerCase() != $.trim(b).toLowerCase()) && $.trim(b) != "";
}




function titlecase(str) {
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}


function preload(arrayOfImages) {
    $(arrayOfImages).each(function() {
    $('<img/>')[0].src = this;
     // Alternatively you could use:
     // (new Image()).src = this;
    });
}

function ins(id, e) {
    node = e.target;
    count = 0;
    while (node != null && node.tagName != "HTML") {
        if (node.id == id) return true;
        node = node.parentNode;
        if (count++ > 100) return false;
    }
    
    return false;
}


$('html').click(function(e) {
                
    if (ins("pobox_category_box", e) || ins("pobox_category_menu_handle", e) ||  ins("pobox_category_menu_shaft", e)){ 
                
        //pobox_ext_menu_hide();
        pobox_locator_menu_hide();
        pobox_category_menu_show();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        main_menu_hide();
        geolocation_header_hide();

    } else if (ins("main_header_text", e) || ins("main_menu", e)) {      

        pobox_ext_menu_hide();
        pobox_locator_menu_hide();
        pobox_category_menu_hide();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        main_menu_show();   
        geolocation_header_hide();

    } else if (ins("pobox_more_button", e)) {
               
        pobox_ext_menu_toggle();
        pobox_locator_menu_hide();
        pobox_category_menu_hide();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        //pobox_hide_error_box();
        main_menu_hide();
        geolocation_header_hide();
               
    } else if (ins("pobox_seller_locator", e) || ins("pobox_locator_menu", e)
             ||ins("pobox_locator_scroll_left", e) || ins("pobox_locator_scroll_right", e)) {
               
        pobox_ext_menu_hide();
        //pobox_locator_menu_hide();
        pobox_category_menu_hide();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        main_menu_hide();
        geolocation_header_hide();
               
    } else if (ins("pobox_ext_menu", e)) {

        pobox_ext_menu_show();
        pobox_locator_menu_hide();
        pobox_category_menu_hide();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        main_menu_hide();
        geolocation_header_hide();

               
    } else {
        pobox_ext_menu_hide();
        pobox_locator_menu_hide();
        pobox_category_menu_hide();
        pobox_places_suggest_hide();
        pobox_seller_suggest_hide();
        pobox_item_name_suggest_hide();
        geolocation_suggest_hide();
        main_menu_hide();
        geolocation_header_hide();
    }
});


$(window).resize(function() {
    pobox_ext_menu_hide()
    pobox_locator_menu_hide();
    pobox_category_menu_hide();
    pobox_places_suggest_hide();
    pobox_seller_suggest_hide();
    pobox_item_name_suggest_hide();
    geolocation_suggest_hide();
    if (!pobox_suppress_warning_box_hide) pobox_hide_error_box(1);
    main_menu_hide();
    geolocation_header_hide(1);
});


/* 
 * ---------------------------------------------------------------------
 * Init
 * ---------------------------------------------------------------------
 */
initialized = 0;

function init() {

    if (initialized) return;

    $("#map_ctrl" ).click(function(e){ main_menu_set_view(VIEW_MAP);});
    $("#grid_ctrl").click(function(e){ main_menu_set_view(VIEW_GRID);});
    $("#list_ctrl").click(function(e){ main_menu_set_view(VIEW_LIST);});

    main_menu_set_view(main_view);
    
    $("#main_container_space").hover(show_friend_feed_collapse_ctrl, hide_friend_feed_collapse_ctrl);
    
    pobox_init(1);

  
    pobox_init_spamfilter();
 
    initialized = 1;



};


$(document).ready(function() {

    init();

    session_start();
     
});



/* 
 * ---------------------------------------------------------------------
 * Session/Geolocation
 * ---------------------------------------------------------------------
 */

user_latitude = null;
user_longitude = null;
user_location = null;



function session_start() {

    $.ajax({url: "php/session.php?" + Math.random() , dataType: "json"})    
    .done(function(s) {

        session_open_variables(s);

        geolocation();
       
    })
    .fail(function(s) {  

        geolocation();
  

    });

}



function session_save_variables() {

    sessvars = "";

    sessvars += "user_location   = \"" + user_location + "\"; " +
                "user_latitude   = " + user_latitude + "; " +
                "user_longitude  = " + user_longitude + "; " ;
  
    sessvars += "pobox_disable_locator_map_during_post  = " + pobox_disable_locator_map_during_post + "; ";

    



    $.ajax("php/session.php?save&variables=" + escape(sessvars));


}


function session_open_variables(s) { 

    eval(s);

}



function geolocation() {


    if (user_location != null && user_latitude != null && user_longitude != null) { 

        geolocation_header_show();
        get_deals();

        return;
    }

   
    $.ajax({url: "php/geolocation.php?" + Math.random() , dataType: "json"}) 

    .done(function(g) {

        if (g != null) {

            user_location = titlecase(g.location);
        
            /*
             * Our geolocation API gives us city/lat/lng, but we will only trust the "city" 
             * part of it.  lat/lng we will get from Google because Google is more accurate! 
             *  
             * TODO: geocode cache on server side! 
             *  
             */
            pobox_geocoder.geocode( { 'address': g.location}, function(results, status) {

                  if (status == google.maps.GeocoderStatus.OK) {
                      user_latitude = results[0].geometry.location.lat();
                      user_longitude = results[0].geometry.location.lng();
                  } else {
                      /*
                       * TODO: error
                       */
                  }

                  geolocation_header_show();
                  get_deals();

            });


        } else {

            // could not get location

            geolocation_setup_location_input();


        }
       

    })
    .fail(function(d) {

        // could not get location

        geolocation_setup_location_input();


    });

}




function geolocation_get_city(v) {

    p = 0;
 
 
    if ((p = v.indexOf(",", 0)) > 0) {
        return $.trim((v.substring(0, p)));
    } else {
        return $.trim(v);
    }

}
 
function geolocation_header_show() {

    hdr = $("#geolocation_header");
    mhdr = $("#main_header");
    tbl = $("#geolocation_header_table");
    gtxt = $("#geolocation_header_location");

    gtxt.html("<strong>" + titlecase(geolocation_get_city(user_location)) + "</strong>");

    mhdrp = mhdr.position();  

    hdr.css("position", "absolute");
    hdr.css({left: mhdrp.left, top: mhdrp.top -2 , height: 22});
    hdr.show();
     
    hdr.width(tbl.width());
    hdr.css({left: mhdrp.left+ mhdr.width() - hdr.width() + 6 } );
     
 


}


function geolocation_header_hide(fast) {

    hdr = $("#geolocation_header");
    if (fast) hdr.hide(); else hdr.fadeOut(200);
 
}


function geolocation_header_edit() {

    geolocation_header_hide(1);
 
    geolocation_setup_location_input();




}



/*
 *----------------------------------------------------------------------------- 
 * BEGIN COOKIE-CUTTER CODE FOR GEOLOCATION AUTOCOMPLETE (so badly need to
 * write a library for this!)
 *-----------------------------------------------------------------------------
 */

geolocation_suggestions = null;
geolocation_suggested_location = null;
geolocation_suggestion_shown = false;

function geolocation_assign_suggestion(i)  {
    $("#geolocation_input").attr('value',  $('#geolocation_suggestion_' + i ).html());
    geolocation_location = $("#geolocation_input").attr('value');

    geolocation_verify_and_set_location_input();
}

function geolocation_suggest_show(p) { 

    geolocation_suggestions = p;
    geolocation_suggest_index = -1;

    box = $("#geolocation_suggest");
    loc = $("#geolocation_input");
    locpos = loc.position();
    tab = $("#geolocation_suggest_table");
    box.css("position", "absolute");
    box.css({left: locpos.left, top: locpos.top + loc.height() + 8, width: 280});

    tab.html('');

    if (p.length == 0) { 
        geolocation_suggestion_shown = false;
        box.hide(); return;
    }

    for (i = 0; i < p.length ; i++) {
        row = $("<tr><td  id='geolocation_suggestion_" + i +  "' style='cursor:pointer' \
                onmouseover=\"geolocation_clear_suggestion_hilite();    geolocation_suggest_index = " + i + "; \
                            $('#geolocation_suggestion_" + i +  "').css('background-color', '#DDDDDD')\"  \
                onclick=\"geolocation_assign_suggestion (" + i +  ");\" >" + p[i] + "</td><tr>");
        tab.append(row);
    }
    box.show();

    geolocation_suggestion_shown = true;
}

function geolocation_suggest_hide(p) {

    geolocation_suggestions = null;
    geolocation_suggest_index = -1;
    box = $("#geolocation_suggest");    
    box.hide();
    geolocation_suggestion_shown = false;
}


geolocation_suggest_suppress = 0;
geolocation_suggest_index = -1;

function geolocation_clear_suggestion_hilite() {
    for (i = 0; i < 3; i++) {  $('#geolocation_suggestion_' + i ).css('background-color', '#FFFFFF'); }
}
 
function geolocation_navigate_suggestions(e) { 

    var val = $("#geolocation_input").attr("value"); 


    if (e.keyCode == 13) {
  
        if ($.trim(val) != "" && geolocation_suggestion_shown == false) { 
            geolocation_verify_and_set_location_input();
            return;
        }
        
    }
    
    geolocation_hide_error_box(1); 
     
    geolocation_clear_suggestion_hilite();

    if (e.keyCode == 38) {
        if (geolocation_suggest_index == -1) return;
        geolocation_suggest_index--;
        $('#geolocation_suggestion_' + geolocation_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 40) {

        if (geolocation_suggest_index == 3) return;
        geolocation_suggest_index++;
        $('#geolocation_suggestion_' + geolocation_suggest_index ).css('background-color', '#DDDDDD');

    } else if (e.keyCode == 13 || e.keyCode == 9 || e.keyCode == 27) {   
        if (geolocation_suggest_index == -1) {
            if (e.keyCode == 9 || e.keyCode == 27) {
                //if (geolocation_suggest_pending) { geolocation_suggest_suppress = 1;  }
                geolocation_suggest_hide();
                return;
            } else {
                return;
            }
        }
        geolocation_assign_suggestion(geolocation_suggest_index);
        geolocation_suggest_hide();
        geolocation_suggest_suppress = 1;
    } else {
        geolocation_suggest_index = -1;
    }
}

/*
 *----------------------------------------------------------------------------- 
 * END COOKIE-CUTTER CODE FOR GEOLOCATION AUTOCOMPLETE 
 *-----------------------------------------------------------------------------
 */

geolocation_location = null;

function geolocation_location_suggest() { 

    val = $("#geolocation_input").attr("value");


    if ($.trim(val) != "") {
        $("#geolocation_submit").show();
    } else {
        $("#geolocation_submit").hide();
    }

 
    if ($.trim(val) == "") {
        geolocation_suggest_hide();
    }

    if ($.trim(val).length >= 3 && $.trim(val).length < 20 && strnew(geolocation_location, val)) {
        geolocation_suggest_search(val);
        pobox_suggested_seller_name = null;
        
    }
   
    pobox_seller_name = val;
 
}
 
function geolocation_suggest_search(s) { 

    $.ajax({url: "php/places.php?search=" + escape(s) , dataType: "json"})
    .done(  geolocation_search_success )
    .fail(  geolocation_search_failure );  

}

function geolocation_search_success(s) { 

    geolocation_suggest_show(s);
}

function geolocation_search_failure(s) { 


}


function geolocation_show_error_box(m) {
 
    geo_error = $("#geolocation_error_box");
    geo_error.attr("class", "errorbox");
    
    tab = $("#geolocation_error_table");
    tab.attr("class", "errorboxtext");

    location_box = $("#geolocation_input");
    topbox = $("#geolocation_error_top_left_corner");
    toppos = topbox.position();
    locpos = location_box.position();
   

    tab.html('');
 
    for (i = 0; i < m.length; i++) {
        row = $("<tr><td valign='middle' width='3%'>" + 
                (m[i].charAt(0) == "!" ? "&nbsp;</td>" : "<img src='images/red.png'></td>"  )  + 
                "<td>"+(m[i].charAt(0) == "!" ? m[i].substring(1) : m[i] )+"</td></tr>");
        tab.append(row);
    }


    geo_error.css("position", "absolute");
    geo_error.css({left: toppos.left, top: toppos.top+1});
    geo_error.show();

    geo_error.css({width: 278, height: tab.height()+10});
    geo_error.show();
 
}

function geolocation_hide_error_box(fast) {

    geo_error = $("#geolocation_error_box");

    if (fast) geo_error.hide(); else geo_error.fadeOut(100);

}


function geolocation_verify_and_set_location_input() {

    var addr = $("#geolocation_input").attr("value");

    m = [];

    $.ajax({url: "php/places.php?search=" + escape(addr) , dataType: "json"})
    .done( function (results) { 

        if (results.length == 0) {

            m.push("!<strong>Location</strong> was not found");
            geolocation_show_error_box(m);
            return;

        } else {

            var found = 0;
            var j = 0;
            for (j = 0; j < results.length; j++) {
                res = results[j];
                if ($.trim(res).toLowerCase() == $.trim(addr).toLowerCase()) {
                    found = 1;
                    break;
                }
            }

            if (!found) {

                m.push("!<strong>Location</strong> wrong. Did you mean:");

                for (j = 0; j < results.length; j++) {
                    m.push("!"+"<span class='pobox_error_location'   onclick=\" $('#geolocation_input').attr('value', '"+
                    results[j]+"'); geolocation_hide_error_box();  geolocation_verify_and_set_location_input();  \"  >"+results[j]+"</span>");
                }

                geolocation_show_error_box(m);
                return;
            }

        }

        pobox_geocoder.geocode( { 'address': addr}, function(results, status) {
    
          if (status == google.maps.GeocoderStatus.OK) {


              user_location = addr;
              user_latitude = results[0].geometry.location.lat();
              user_longitude = results[0].geometry.location.lng();

              geolocation_set_location_input();
 
    
          } else {
              
              if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                  m.push("!<strong>Location</strong> is invalid");
                  geolocation_show_error_box(m);
              }
          }
        });


     })
    .fail( function () { 
    
        m.push("Server Error (geolocation)");
        geolocation_show_error_box(m);
        return;    
    
    });
}


function geolocation_setup_location_input() {


    $("#main_container").load("html/geolocation.html?" + Math.random() , function() {


        $("#geolocation_container").fadeIn(200);
        $("#geolocation_input").focus();

        // attach autocomplete handlers for the input box

        $("#geolocation_input").typing({ stop: geolocation_location_suggest, delay: 0 });
        $("#geolocation_input").keydown( geolocation_navigate_suggestions );
        $("#geolocation_input").focus( function() { geolocation_hide_error_box(0); } );
    });

}

function geolocation_set_location_input() {

    // save the user location in the session 

    $("#main_container").html("<div align='center'><img src='images/loader3.gif'></div>");

    session_save_variables();

    geolocation();
}


/* 
 * ---------------------------------------------------------------------
 * Main Menu/Container
 * ---------------------------------------------------------------------
 */

deals = null;
deals_index = 0;

function main_show_first_page() {


    $("#main_container").load((logged_in ? "html/first-login.html?" : "html/first.html?")  + Math.random(), function (html) {

        $("#first-notice").fadeIn(300);

        main_container_adjust_for_ie()

        $("#first_city").html(geolocation_get_city(user_location));

    });

}


function get_deals() {

    $("#main_container").html("<div align='center'><img src='images/loader3.gif'></div>");

    $.ajax({url: "php/deals.php?lat="+user_latitude+"&lng="+user_longitude+"&r=" + Math.random() , dataType: "json"})
    .done(function(d) {

        deals = d;
        //deals = [];

        if (deals.length == 0) {
            main_show_first_page();
            return;
        }

      
        setTimeout("main_container_update_view();", 50);

        

    })
    .fail(function(d) {


    });

}


function main_update_map() {

}


function main_grid_scrollto(d) {


    if (friend_feed_shown) {
        

    } else {
       

    }

    main_update_grid();
}


grid_view_expanded = 0;


function main_container_adjust_for_ie() {

    var c = $("#main_container");
    var g = $("#grid-cells");
    var f = $("#first-notice");

    if (Math.abs(c.width() - g.width()) > 5) {
        g.width(c.width());
    }

    if (Math.abs(c.width() - f.width()) > 5) {
        f.width(c.width());
    }
}

setInterval("main_container_adjust_for_ie()", 1000);

function main_grid_cell_show_controls(id) { 

    //controls = $("#grid-cell-controls-"+id);
    //controls.show();

}

function main_grid_cell_hide_controls(id) {

    //controls = $("#grid-cell-controls-"+id);
    //controls.hide();

}

function show_info_box(top, element, html, w, h) {

   
    box = $("#main_grid_info_box");

    elepos = element.position();

    if (top == false) {

        tip = $("#main_grid_info_bottom_tip");
    
    
        tip.css("position", "absolute");
        tip.css({top: elepos.top + element.height() + 1, left: elepos.left + element.width()/2 - 2});
        tip.width(9);
        tip.height(5);
        tip.show();
    
        tippos = tip.position();
        
        box.css("position", "absolute");
        box.css({top: tippos.top + 4, left: tippos.left - 10});
        box.width(w);
        box.height(h);
        
        box.show();

    } else {

        tip = $("#main_grid_info_top_tip");

        tip.appendTo(element);
        box.appendTo(element);
 
        box.css("position", "absolute");
        box.css({top: elepos.top - 5 - h , left: elepos.left + element.width()/2 - 12});
        box.width(w);
        box.height(h);
        
        box.fadeIn(200);

        tip.css("position", "absolute");
        tip.css({top: box.offset().top + box.height() + 1, left: elepos.left + element.width()/2 - 2});
        tip.width(9);
        tip.height(5);
        tip.fadeIn(200);

        tippos = tip.position();




    }

}


function hide_info_box() {

    tip = $("#main_grid_info_top_tip");
    tip.fadeOut(200);

    tip = $("#main_grid_info_bottom_tip");
    tip.fadeOut(200);

    box = $("#main_grid_info_box");
    box.fadeOut(200);

    
    
    
    


}


function main_grid_cell_show_seller_info(id) {



}


function main_fill_grid(n) { 

    if (deals == null)  return;

    slice = deals.slice(deals_index);

    m = $("#main_container");  

    m.load("html/grid-"+n+".html?" + Math.random(), function (grid) {

       main_container_adjust_for_ie();

       $.get("html/cell.html?" + Math.random() , function (text) {

           cc = 0;
           x = text;

           for (i = 0; i < n; i++) {

               if (slice[i] == null) {
                   continue;
               }

               if (cc < n/2-1) {
                   x = text.replace("BORDER", "cell_1");
               } else if (cc == n/2-1) {
                   x = text.replace("BORDER", "cell_3");
               } else if (cc == n-1) {
                   x = text.replace("BORDER", "BORDER");
               } else {
                   x = text.replace("BORDER", "cell_7");
               }

               x = x.replace('XXX', slice[i].id);
               x = x.replace('XXX', slice[i].id);
               x = x.replace('XXX', slice[i].id);
               x = x.replace('XXX', slice[i].id);
               x = x.replace('XXX', slice[i].id);


               x = x.replace("IMAGE", "<img style='cursor:pointer;' src='"+slice[i].photo+"' height='100'>");

               if (slice[i].value != 0) {
                   x = x.replace("PRICE", "<nobr style='cursor:default;'>$" + slice[i].value+"</nobr>");
               } else {
                   x = x.replace("PRICE", "<nobr style='cursor:default;'>FREE</nobr>");
               }

               foo = "<span style='cursor:default'>";

               if (slice[i].name != null) {
                   foo += "<nobr><span style='color:#000000'>" + titlecase(slice[i].name) + "</span></nobr>";
               } else {
                   foo += "Someone";
               }

               if (slice[i].online != null) {
                   foo += " in ";
                   foo += "<nobr><span>" + titlecase(geolocation_get_city(slice[i].location)) + "</span></nobr>";
               }

               if (slice[i].value != 0) {
                   foo += " bought ";
               } else {
                   foo += " got ";
               }
               foo += "<nobr><span style='color:#FF0000'>" + titlecase(slice[i].item) + "</span></nobr>";
               foo += " from ";
               foo += "<nobr><span id='grid-cell-seller-"+slice[i].id+"'><span class='text'>" + titlecase(slice[i].seller) + "</span></span></nobr>";

               if (slice[i].online == null) {
                   foo += " in ";
                   foo += "<nobr><span>" + titlecase(geolocation_get_city(slice[i].location)) + "</span></nobr>";
               } 

               foo += " <span style='color:#bbbbbb;'><nobr>" + elapsedstring(slice[i].elapsed_time) + " ago</nobr></span>";

               foo += "</span>";

               x = x.replace("FOOTER", foo);

               cell = $("#grid-cell-" + i); 

               cell.html(x);

               $('#grid-box-'+slice[i].id).hoverIntent({
               over: function () {

                    $(this).find(".controls").fadeIn(200);
               },  
               out: function () {

                    $(this).find(".controls").hide();
                    

               }, timeout: 100});


               $('#grid-cell-seller-'+slice[i].id).hoverIntent({
               over: function () {

                   $text = $(this).find(".text");

                   show_info_box(true, $text, "test", 150, 100);

               },  
               out: function () {

                    hide_info_box();

               }, timeout: 100});


               

               cc++;

               $(".grid-cell").fadeIn(200);

           }

       });
      
   });

}


function main_update_grid() {

    if (friend_feed_shown) { 

        main_fill_grid(6);


    } else if (grid_view_expanded) {


    } else {
      
        main_fill_grid(8);

    }

}

function main_populate_list() {

}

function main_container_clear_view() {

    $("#main_container").html("");
}

function main_container_update_view() {
 
 
    if (main_view == VIEW_MAP) {
        main_update_map();
    }

    if (main_view == VIEW_GRID) {
        main_update_grid();
    }


}

function main_menu_show() {
    cont = $("#main_container");
    cpos = cont.offset();
    span = $("#main_header_text");
    header = $("#main_header");
    hpos = header.offset();
    mheader = $("#main_menu_header");
    menu = $("#main_menu");
    
    w = 160;
    
    mheader.css("position", "absolute");
    mheader.css({top: cpos.top - 4, left: cpos.left + cont.width() - w});
    mheader.width(w);
    mheader.height(header.outerHeight()+3);
    mheader.show();
    
    
    menu.css("position", "absolute");
    menu.css({top: hpos.top + mheader.height() , left: cpos.left + cont.width() - w});
    menu.width(w);
    
    menu.show();
    
}


function main_menu_hide() {
    mheader = $("#main_menu_header");
    menu = $("#main_menu");
    aux = $("#main_menu_aux");
    
    mheader.hide();
    menu.hide();
    
}

VIEW_MAP = 0;
VIEW_GRID = 1;
VIEW_LIST = 2;

main_view = VIEW_GRID;

function main_menu_set_view(v) {

    if (v != VIEW_MAP && v != VIEW_LIST && v != VIEW_GRID) return;

    map_c = $("#map_ctrl");
    grid_c = $("#grid_ctrl");
    list_c = $("#list_ctrl");
    
    if (v == VIEW_MAP) {
        map_c.css("border",  "solid 1px #DDDDDD");
        grid_c.css("border", "solid 1px #FFFFFF");
        list_c.css("border", "solid 1px #FFFFFF");
    }

    if (v == VIEW_GRID) {
        map_c.css("border",  "solid 1px #FFFFFF");
        grid_c.css("border", "solid 1px #DDDDDD");
        list_c.css("border", "solid 1px #FFFFFF");
    }

    if (v == VIEW_LIST) {
        map_c.css("border",  "solid 1px #FFFFFF");
        grid_c.css("border", "solid 1px #FFFFFF");
        list_c.css("border", "solid 1px #DDDDDD");      
    }

        
    main_view = v;

    main_container_update_view();

}


/* 
 * ---------------------------------------------------------------------
 * Post-box / input validation
 * ---------------------------------------------------------------------
 */




//----------------------------------------------------------------------
// END POBOX MAP LOCATOR
//----------------------------------------------------------------------










pobox_expanded = 0;

function pobox_ext_menu_hide() {
    
    pobox_category_menu_hide();
    $("#pobox_more_text").html("More");
    footer = $("#pobox_ext_footer");
    menu = $("#pobox_ext_menu");
    
    footer.fadeOut(200);
    menu.fadeOut(200);
    
    pobox_expanded = 0;
}

function pobox_ext_menu_show() {
    $("#pobox_more_text").html("Less");
    
    cont = $("#main_container");
    footer = $("#pobox_ext_footer");
    menu = $("#pobox_ext_menu");
    more = $("#pobox_more_button");
    pobox = $("#pobox_inputs"); 
    
    menu.css("position", "absolute");
    menu.css({left: cont.offset().left +15, top: more.offset().top - 10, width: 320});
    menu.fadeIn(100);
    menu.css({top: more.offset().top - 10 - menu.height()});
    
    footer.css("position", "absolute");
    footer.css({left: more.offset().left, top: menu.offset().top + menu.height()+1, width: more.width(), height: 10});
    footer.fadeIn(100);
    
    pobox_expanded = 1;
}

function pobox_ext_menu_toggle() {
    if (pobox_expanded) {
        pobox_ext_menu_hide();
    } else {
        pobox_ext_menu_show();
    }
}


function pobox_ext_menu_visibility_row_show() {
    pobox_ex = pobox_expanded;
    if (pobox_ex) pobox_ext_menu_hide();
    $('.pobox_vrow').css("display", "block");
    $('.pobox_vpad').css("display", "none");
    if (pobox_ex) pobox_ext_menu_show();

}

function pobox_ext_menu_visibility_row_hide() {
    pobox_ex = pobox_expanded;
    if (pobox_ex) pobox_ext_menu_hide();
    $('.pobox_vrow').css("display", "none");
    $('.pobox_vpad').css("display", "block");
    if (pobox_ex) pobox_ext_menu_show();

}



pobox_category_menu_expanded = 0;

function pobox_category_menu_show() {
    box = $("#pobox_category_box");
    boxpos = box.position();
    menu = $("#pobox_category_menu");

    menu.css("position", "absolute");
    menu.css({left: boxpos.left, top: boxpos.top + box.height() + 3});
    menu.css({width: box.outerWidth()});
    menu.show();
    
    menupos = menu.offset();

    
    handle = $("#pobox_category_menu_handle");
    handle.css("position", "absolute");
    handle.css({left: menu.width() - 10, top:  1});
    handle.css({width: "9"});
    handle.show();
    
}

function pobox_category_menu_hide() {
    menu = $("#pobox_category_menu");
    menu.hide();
}


/*
 *-----------------------------------------------------------------------------
 * BEGIN POBOX itemname AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */

function pobox_item_value_hide_line() {
  
    $("#pobox_item_name_input").attr("class", "pobox_input_tmp");

    //$("#pobox_item_value_input").css("background", "#FFFFFF url(images/dblank.png) no-repeat 4px 4px");
    
}

function pobox_item_value_show_line() {

    if ($("#pobox_item_name_input").attr("class") == 'pobox_input_initial') return;

    $("#pobox_item_name_input").attr("class", "pobox_input");

    $("#pobox_item_value_input").css("background", "#FFFFFF url(images/dollar.png) no-repeat 4px 4px");
}


function pobox_item_name_assign_suggestion(i, wait)  {
    $("#pobox_item_name_input").attr('value',  $('#pobox_item_name_suggestion_' + i ).html());
    pobox_item_name = $("#pobox_item_name_input").attr('value');

    setTimeout('pobox_item_image_update();', wait ? 1000 : 0);
}


function pobox_item_name_suggest_show(p) { 
    pobox_item_name_suggest_index = -1;

    box = $("#pobox_item_name_suggest");
    loc = $("#pobox_item_name_input");
    locpos = loc.position();
    tab = $("#pobox_item_name_suggest_table");
    box.css("position", "absolute");
    box.css({left: locpos.left, top: locpos.top + loc.height() + 4, width: 240});

    tab.html('');

    if (p.length == 0) { box.hide(); return;
        row = $("<tr><td>" + 'No such place' + "</td><tr>");
        tab.append(row);
    }

    for (i = 0; i < p.length ; i++) {
        row = $("<tr><td  id='pobox_item_name_suggestion_" + i +  "' style='cursor:pointer' \
                onmouseover=\"pobox_clear_item_name_suggestion_hilite();    pobox_item_name_suggest_index = " + i + "; \
                            $('#pobox_item_name_suggestion_" + i +  "').css('background-color', '#DDDDDD')\"  \
                onclick=\"pobox_item_name_assign_suggestion (" + i +  ", 1);\" \
                 >" + titlecase(p[i]) + "</td><tr>");
        tab.append(row);
    }
    box.show();
 
    pobox_item_value_hide_line();
}

function pobox_item_name_suggest_hide(p) {
    pobox_item_name_suggest_index = -1;
    box = $("#pobox_item_name_suggest");    
    box.hide();
    
    pobox_item_value_show_line();
}

pobox_item_name_suggest_suppress = 0;
pobox_item_name_suggest_index = -1;

function pobox_clear_item_name_suggestion_hilite() {
    for (i = 0; i < 4; i++) {  $('#pobox_item_name_suggestion_' + i ).css('background-color', '#FFFFFF'); }
}

function pobox_navigate_item_name_suggestions(e) {  
    pobox_clear_item_name_suggestion_hilite();
    if (e.keyCode == 38) {
        if (pobox_item_name_suggest_index == -1) return;
        pobox_item_name_suggest_index--;
        $('#pobox_item_name_suggestion_' + pobox_item_name_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 40) {

        if (pobox_item_name_suggest_index == 4) return;
        pobox_item_name_suggest_index++;
        $('#pobox_item_name_suggestion_' + pobox_item_name_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 13 || e.keyCode == 9 || e.keyCode == 27) {   
        if (pobox_item_name_suggest_index == -1) {
            if (e.keyCode == 9 || e.keyCode == 27) {
                pobox_item_name_suggest_suppress = 1; 
                pobox_item_name_suggest_hide();
                return;
            } else {
                return;
            }
        }
        pobox_item_name_assign_suggestion(pobox_item_name_suggest_index, 0);
        pobox_item_name_suggest_hide();
        pobox_item_name_suggest_suppress = 1;
    } else {
        pobox_item_name_suggest_index = -1;
    }
}

/* 
 *-----------------------------------------------------------------------------
 * END-POBOX itemname AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */




/*
 *-----------------------------------------------------------------------------
 * BEGIN POBOX PLACES AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */
function pobox_places_assign_suggestion(i)  {
    $("#pobox_location_input").attr('value',  $('#pobox_places_suggestion_' + i ).html());
    pobox_location = $("#pobox_location_input").attr('value');


    // TODO: pobox_preload_locator_search();
}

function pobox_places_suggest_show(p) {
    pobox_places_suggest_index = -1;

    box = $("#pobox_places_suggest");
    loc = $("#pobox_location_input");
    locpos = loc.position();
    tab = $("#pobox_places_suggest_table");
    box.css("position", "absolute");
    box.css({left: locpos.left, top: locpos.top + loc.height() + 4, width: 240});

    tab.html('');

    if (p.length == 0) { box.hide(); return;
        row = $("<tr><td>" + 'No such place' + "</td><tr>");
        tab.append(row);
    }

    for (i = 0; i < p.length ; i++) {
        row = $("<tr><td  id='pobox_places_suggestion_" + i +  "' style='cursor:pointer' \
                onmouseover=\"pobox_clear_places_suggestion_hilite();    pobox_places_suggest_index = " + i + "; \
                            $('#pobox_places_suggestion_" + i +  "').css('background-color', '#DDDDDD')\"  \
                onclick=\"pobox_places_assign_suggestion (" + i +  ");\" \
                 >" + p[i] + "</td><tr>");
        tab.append(row);
    }
    box.show();
 

}

function pobox_places_suggest_hide(p) {
    pobox_places_suggest_index = -1;
    box = $("#pobox_places_suggest");    
    box.hide();
}

pobox_places_suggest_suppress = 0;
pobox_places_suggest_index = -1;

function pobox_clear_places_suggestion_hilite() {
    for (i = 0; i < 3; i++) {  $('#pobox_places_suggestion_' + i ).css('background-color', '#FFFFFF'); }
}

function pobox_navigate_places_suggestions(e) {
    pobox_clear_places_suggestion_hilite();
    if (e.keyCode == 38) {
        if (pobox_places_suggest_index == -1) return;
        pobox_places_suggest_index--;
        $('#pobox_places_suggestion_' + pobox_places_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 40) {

        if (pobox_places_suggest_index == 3) return;
        pobox_places_suggest_index++;
        $('#pobox_places_suggestion_' + pobox_places_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 13 || e.keyCode == 9 || e.keyCode == 27) {   
        if (pobox_places_suggest_index == -1) {
            if (e.keyCode == 27) {
                pobox_places_suggest_suppress = 1; 
                pobox_places_suggest_hide();
                return;
            }
        }
        if (pobox_places_suggest_index == -1) return;
        pobox_places_assign_suggestion(pobox_places_suggest_index);
        pobox_places_suggest_hide();
        pobox_places_suggest_suppress = 1;
    } else {
        pobox_places_suggest_index = -1;
    }
}

/* 
 *-----------------------------------------------------------------------------
 * END-POBOX PLACES AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */

/*
 *-----------------------------------------------------------------------------
 * BEGIN POBOX SELLER AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */
pobox_seller_suggestions = null;

pobox_suggested_seller_name = null;

function pobox_seller_assign_suggestion(i)  {
    $("#pobox_seller_input").attr('value',  $('#pobox_seller_suggestion_' + i ).html());
    pobox_seller_name = $("#pobox_seller_input").attr('value');

    if (typeof pobox_seller_suggestions[i] == 'object') {
        if (pobox_seller_suggestions[i]['online'] > 0) {
            pobox_set_online_seller();
        }
    }

    pobox_seller_image_update();
}

function pobox_seller_suggest_show(p) { 

    if (pobox_seller_lastkey == 9 && pobox_seller_suggest_pending)  { return; }
 
    pobox_seller_suggestions = p;
    pobox_places_suggest_index = -1;

    box = $("#pobox_seller_suggest");
    loc = $("#pobox_seller_input");
    locpos = loc.position();
    tab = $("#pobox_seller_suggest_table");
    box.css("position", "absolute");
    box.css({left: locpos.left, top: locpos.top + loc.height() + 4, width: 240});

    tab.html('');

    if (p.length == 0) { 
        box.hide(); return;
    }

    for (i = 0; i < p.length ; i++) {
        row = $("<tr><td  id='pobox_seller_suggestion_" + i +  "' style='cursor:pointer' \
                onmouseover=\"pobox_clear_seller_suggestion_hilite();    pobox_seller_suggest_index = " + i + "; \
                            $('#pobox_seller_suggestion_" + i +  "').css('background-color', '#DDDDDD')\"  \
                onclick=\"pobox_seller_assign_suggestion (" + i +  ");\" \
                 >" + (typeof p[i] == 'object' ? titlecase(p[i]['name']) : titlecase(p[i])) + "</td><tr>");
        tab.append(row);
    }
    box.show(); 
}

function pobox_seller_suggest_hide(p) {

    var online = 0;
    v = $("#pobox_seller_input").attr('value');

    if (pobox_seller_suggestions != null) {

        for (x = 0; x < pobox_seller_suggestions.length; x++) { 

            sel = (typeof pobox_seller_suggestions[x] == 'object' ? pobox_seller_suggestions[x]['name'] : pobox_seller_suggestions[x]);  
 
            sel = $("<div/>").html(sel).text();

            if (($.trim(v.toLowerCase())) == unescape($.trim(sel.toLowerCase()))) {
                pobox_suggested_seller_name = $.trim(v.toLowerCase());
                online = (typeof pobox_seller_suggestions[x] == 'object' ? pobox_seller_suggestions[x]['online'] : false);
                break;
            }
        }
    }

    if ($.trim(pobox_suggested_seller_name).toLowerCase() == $.trim(v).toLowerCase()) {
        if (online > 0) {
            pobox_set_online_seller();
        }
    }

    pobox_seller_suggestions = null;
    pobox_seller_suggest_index = -1;
    box = $("#pobox_seller_suggest");    
    box.hide();
}

pobox_seller_suggest_suppress = 0;
pobox_seller_suggest_index = -1;

function pobox_clear_seller_suggestion_hilite() {
    for (i = 0; i < 3; i++) {  $('#pobox_seller_suggestion_' + i ).css('background-color', '#FFFFFF'); }
}

pobox_seller_lastkey = 0;

function pobox_navigate_seller_suggestions(e) { 
    pobox_seller_lastkey = e.keyCode;
    pobox_clear_seller_suggestion_hilite();
    if (e.keyCode == 38) {
        if (pobox_seller_suggest_index == -1) return;
        pobox_seller_suggest_index--;
        $('#pobox_seller_suggestion_' + pobox_seller_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 40) {

        if (pobox_seller_suggest_index == 3) return;
        pobox_seller_suggest_index++;
        $('#pobox_seller_suggestion_' + pobox_seller_suggest_index ).css('background-color', '#DDDDDD');
    } else if (e.keyCode == 13 || e.keyCode == 9 || e.keyCode == 27) {   
        if (pobox_seller_suggest_index == -1) {
            if (e.keyCode == 9 || e.keyCode == 27) {
                if (pobox_seller_suggest_pending) { pobox_seller_suggest_suppress = 1;  }
                pobox_seller_suggest_hide();
                return;
            } else {
                return;
            }
        }
        pobox_seller_assign_suggestion(pobox_seller_suggest_index);
        pobox_seller_suggest_hide();
        pobox_seller_suggest_suppress = 1;
    } else {
        pobox_seller_suggest_index = -1;
    }
}

/* 
 *-----------------------------------------------------------------------------
 * END-POBOX PLACES AUTOCOMPLETE
 *----------------------------------------------------------------------------- 
 */


function pobox_set_online_seller() {

    if (pobox_location_initial) $("#pobox_location_input").focus();

    pobox_seller_online = 1;
    cl = $("#pobox_location_close");
    cr = $("#pobox_location_close_tr");
    lo = $("#pobox_seller_locator");

    bo = $("#pobox_location_input");
    bo.attr("value", "online");
    bo.attr("disabled", "disabled");
    cl.html("<img src='images/cross.gif' style='cursor:pointer' onclick='pobox_unset_online_seller();'>");
    cr.css("background-color", bo.css('background-color'));
    lo.html("<img  id='pobox_seller_locator_image' src='images/blank.png'>");
}


function pobox_unset_online_seller() {
    pobox_seller_online = 0;
    cl = $("#pobox_location_close");
    cr = $("#pobox_location_close_tr");
    lo = $("#pobox_seller_locator");

    cl.html("<img src='images/blank.png'>");
    bo = $("#pobox_location_input");

    if ($.trim(pobox_location).toLowerCase() == 'online') {
        bo.attr("value", "");
    } else {
        bo.attr("value", pobox_location);
    }
    


    bo.removeAttr("disabled");

    cr.css("background-color", '#FFFFFF');

    lo.html("<img  id='pobox_seller_locator_image' src='images/location.png' style='cursor:pointer' onclick='pobox_seller_locate(0);'>");

}


pobox_visibitility = 0;

function pobox_set_visibility(v) {
    
}


function pobox_fill_random() {

    $.ajax({url: "php/randomdeals.php?" + Math.random() , dataType: "json"})
    .done(function(d) {

        rand = Math.floor(Math.random()*(d.length)); 

        $("#pobox_item_name_input").attr("value", (d[rand].item[0] == ' ' ? '' : ' ') + titlecase(d[rand].item));
        $("#pobox_item_value_input").attr("value", d[rand].value);
        $("#pobox_seller_input").attr("value", (d[rand].seller[0] == ' ' ? '' : ' ') + titlecase(d[rand].seller));
        $("#pobox_location_input").attr("value", (d[rand].location[0] == ' ' ? '' : ' ') + titlecase(d[rand].location));

        pobox_set_item_image(d[rand].item_image);
        pobox_set_seller_image(d[rand].seller_image);

        pobox_item_name_for_image_update = d[rand].item;
        pobox_seller_name_for_image_update = d[rand].seller;

        pobox_item_images = [];
        pobox_item_images.push(d[rand].item_image);
        pobox_item_image_index = 0;

        pobox_seller_images = [];
        pobox_seller_images.push(d[rand].item_image);
        pobox_seller_image_index = 0;
        
    })
    .fail(function(d) {


    });

}



function pobox_post_process(d) { 

    //$(window).resize();


    if (d == null) {

      if($("#pobox_verify_text").length) {

          pobox_verify_box_show_error("Unknown Error");

      } else {

          pobox_show_error_box("Unknown Error");

      }
    
    } else if (d[0] == "captcha") {

        w =  $("#postbox_container").width();

        $("#postbox_container").load('html/pobox_verify.html?' + Math.random(), function() { 

            $(window).resize();

            $('#pobox_captcha_input').keypress(function (e) {

                if (e.keyCode == 13) {
                    pobox_repost();
                }

            });


            $('#pobox_captcha_input').focus(); 

            $('#pobox_captcha_image').attr('src', 'php/captcha.php?op=image&r='+Math.random());


        });

  

    } else if (d[0] == "recaptcha") {

         w =  $("#postbox_container").width();

        pobox_verify_box_show_error("Try again...");

        $("#pobox_captcha_image").attr('src', 'php/captcha.php?op=image&' + Math.random());


        setTimeout("$('#pobox_captcha_input').focus();  ", 400);
       
        
    }  else if (d[0] == "success") {

        $(window).resize();
    
        $("#postbox_container").load('html/pobox_success.html?' + Math.random(), function () {

            $("#pobox_success_item").html(titlecase(d[1])+ " ");

            /*
             * TODO : more formatting here!!!
             */


        });

        get_deals();
        
    } else {

      if($("#pobox_verify_text").length) {

          pobox_repost_error(d);

      } else {

          pobox_show_error_box(d);

      }

    }


    text = $("#pobox_post_button_text");
    text.html("Post");
    text = $("#pobox_post_button2");
    text.html("Post");

 
}


function pobox_verify_cancel() {

    $('#postbox_container').load('html/pobox.html', function() {

        pobox_init(0);
        pobox_restore_context();

        /* 
         * Redraw the locks
         */
        if (pobox_item_image_box_state == IMBOX_LOCKED) {
            pobox_item_image_box_controls_render();
            pobox_hide_item_image_box_ext2();
        }
        if (pobox_seller_image_box_state == IMBOX_LOCKED) {
            pobox_seller_image_box_controls_render();
            pobox_hide_seller_image_box_ext2();
        }
            
    });

}


function pobox_success_ok() {

    $('#postbox_container').load('html/pobox.html', function() {
 
        pobox_init(1);
        pobox_clear_context();


    });

}


 
function pobox_verify_box_show_error(e) {

    te = $("#pobox_verify_text");

    ht = "<nobr><span style='color:#FF0000'><strong>"+e+"</strong></span>";

    te.html(ht);

}

function pobox_verify_box_hide_error() {

    te = $("#pobox_verify_text");

    ht = "<nobr>We need to verify you since you are not logged in...</nobr><br /> <br /> <strong>Login</strong> to skip this hassle";

    te.html(ht);


}

function pobox_post_error(d) {

 
    pobox_show_error_box(d);

    text = $("#pobox_post_button_text");
    text.html("Post");

 
}

function pobox_repost_error(d) {

    pobox_verify_box_show_error(d);


    text = $("#pobox_post_button2");
    text.html("Post");

    bu = $("#pobox_verify_buttons");
    bu.html("<tr><td width='100%' class='pobox_button' id='pobox_cancel_button' onclick='pobox_verify_cancel();'><div align='center' id='pobox_cancel_text'>Cancel</div></td></tr>");
           
}


function pobox_regenerate_captcha() {

    $("#pobox_captcha_reload").attr('src', 'images/loader2.gif');

    $.ajax("php/captcha.php?op=regenerate")

    .done(function() { $("#pobox_captcha_reload").attr('src', 'images/reload.png'); 
                       $("#pobox_captcha_image").attr('src', 'php/captcha.php?op=image&' + Math.random());  })
}


function pobox_repost() {

    $(window).resize();

    text = $("#pobox_post_button2");
    text.html("<img src='images/loader3.gif'>");

    cap = $("#pobox_captcha_input").attr('value');

    $.post("php/post.php?&captcha=" + escape(cap), 

       pobox_filter_context(pobox_context),

       function(data) { pobox_post_process(data); },

       "json"

    ).error(function(data) { pobox_repost_error(); });
}

function pobox_clear_init() {

    if (pobox_item_initial) $("#pobox_item_name_input").attr("value", "").focus();
    if (pobox_item_initial) $("#pobox_item_value_input").attr("value", "").focus();
    if (pobox_seller_initial) $("#pobox_seller_input").attr("value", "").focus();
    if (pobox_location_initial) $("#pobox_location_input").attr("value", "").focus();
    if (pobox_item_initial) $("#pobox_item_name_input").attr("value", "").focus();
    //$("#pobox_item_name_input").focus();

}


function pobox_misc_warn() {

    var value  = $.trim($("#pobox_item_value_input").attr('value'));
    
    if (parseInt(value) == 0) {
        pobox_show_warning_box(["The price of your purchase is <strong>$0</strong>", 
                                "Did you really find it for <strong>free</strong>?",
                                "",
                                "<span onclick='pobox_bypass_misc = true; pobox_verify_and_post();' style='font-size:7pt; cursor:pointer; color:#999999'>Yes. Post this <strong>free</strong> deal!</span>"]);

        return 1;
    }

    return 0;
}


spamwords = null;

function pobox_verify_spam_phrase(input) {

    var word = null;
    var weight = 0;
    var x = 0;
    var s = null;

    input = input.toLowerCase();
    array = input.split(" ");
   
    for (i = 0; i < spamwords.length; i++) {

        s = $.trim(spamwords[i].word).toLowerCase();
        weight = spamwords[i].weight;


        /*
         * For short spam words, we look for the whole words in the input
         */
        if (s.length <= 3) { 
            
            for (j = 0 ; j < array.length; j++) {
                if ($.trim(array[j]).toLowerCase() == $.trim(s).toLowerCase()) {
                    x += weight;
                    
                }
            }

        } else {
            /*
             * For longer spam words, we look for it anywhere in the input
             */
            if (input.indexOf(s) >= 0) {
                x += weight; 
                 
            }
        }
    }

    return x;
}

function pobox_verify_spam() {

    var item   = $.trim($("#pobox_item_name_input").attr('value'));
    var seller = $.trim($("#pobox_seller_input").attr('value'));
    var loc    = $.trim($("#pobox_location_input").attr('value'));
    var link   = $.trim($("#pobox_link_input").attr('value'));
    var desc   = $.trim($("#pobox_description_input").attr('value'));

    sources = [];

    var x = 0;
    var t = 0;

 
    if (item != "") {
        t = pobox_verify_spam_phrase(item); x += t;
        if (t > 0) {
            sources.push("Bought");
        }
    }

    if (seller != "") {
        t = pobox_verify_spam_phrase(seller); x += t;
        if (t > 0) {
            sources.push("From");
        }
    }

    if (loc != "") {
        t = pobox_verify_spam_phrase(loc); x += t;
        if (t > 0) {
            sources.push("Location");
        }
    }

    if (link != "") {
        t = pobox_verify_spam_phrase(link); x += t;
        if (t > 0) {
            sources.push("Link");
        }
    }


    if (desc != "") {
        t = pobox_verify_spam_phrase(desc); x += t;
        if (t > 0) {
            sources.push("Description");
        }
    }

    if (x > 0) {
    
    pobox_show_warning_box(["<span style='font-size:7pt; line-height:150%;'>There are some <strong style=' color:#FF0000;'>inappropriate</strong> words in the following fields:</span>",
                            "<span style='font-size:7pt; font-weight: bold;'>"+sources.join().replace(/,/gi, ", ")+"</span>","",
                            "<span onclick='pobox_bypass_spam = true;; pobox_verify_and_post();' style='font-size:7pt; color:#999999; cursor:pointer;'>Post Anyway</span>"]);
    }
    
   
    return x;
}

function pobox_spamfilter() {

    if (spamwords == null) {
        
        $.ajax({url: "php/spamwords.php" , dataType: "json"}).done(function(spam) {

            spamwords = (spam);

            pobox_show_warning_box(["Temporary Error. Hit </strong>Post</strong> Again."]);

        });


    } else {

        return pobox_verify_spam();
    }
}


function pobox_verify() {

    var item   = $.trim($("#pobox_item_name_input").attr('value'));
    var value  = $.trim($("#pobox_item_value_input").attr('value'));
    var seller = $.trim($("#pobox_seller_input").attr('value'));
    var loc    = $.trim($("#pobox_location_input").attr('value'));
    var link   = $.trim($("#pobox_link_input").attr('value'));
    var desc   = $.trim($("#pobox_description_input").attr('value'));
 
    m = [];

    if (item == "") { 
        m.push("<strong>Bought</strong> box is empty");
    } else if (item.length < 3) {
        m.push("<strong>Bought</strong> box value too short"); 
    } else if (item.length > 64) {
        m.push("<strong>Bought</strong> box value too long"); 
    }

  
    if (value == "") {
        m.push("<strong>Price</strong> box is empty"); 
    } else if (!isNumber(value)) {
        m.push("<strong>Price</strong> value is not a number"); 
    } else if (parseInt(value) > 1000000) {
        m.push("<strong>Price</strong> is way too high =)"); 
    } else if (parseInt(value) < 0) {
        m.push("<strong>Price</strong> cannot be negative"); 
    }
 

    if (seller == "") {
        m.push("<strong>From</strong> box is empty");  
    } else if (seller.length < 2) {
        m.push("<strong>From</strong> box value too short"); 
    } else if (seller.length > 64) {
        m.push("<strong>From</strong> box value too long"); 
    }


    if (loc == "") { 
        m.push("<strong>Location</strong> box is empty"); 
    } else if (loc.length < 3) { 
        m.push("<strong>Location</strong> box value too short");
    } else if (loc.length > 128) {
        m.push("<strong>Location</strong> box value too long");
    }

    if (link.length > 128) {
        m.push("<strong>Link</strong> box value too long");
    }

    if (desc.length > 512) {
        m.push("<strong>Description</strong> box value too long");
    }


    if (m.length > 0) {
        m = m.slice(0, 4);
        pobox_show_error_box(m);
        return 0;
    }

    return 1;
}


pobox_bypass_spam = false;
pobox_bypass_location = false;
pobox_bypass_misc = false;

function pobox_verify_and_post() {

   
    lo = $("#pobox_location_input");
    addr = $.trim(lo.attr('value'));

    pobox_clear_init();

    v = pobox_verify();

    if (!v) {
        return;
    }


    if (addr.toLowerCase() == "online") {

        if (!pobox_bypass_spam) {
            if (pobox_spamfilter()) {
                return;
            }
        }

        if (!pobox_bypass_misc) {
            if (pobox_misc_warn()) {
                return;
            }
        }

        pobox_post();
        return;
    }

    m = [];

    pobox_geocoder.geocode( { 'address': addr}, function(results, status) {
    
      if (status == google.maps.GeocoderStatus.OK) {

          /*
           * if lat/long are not set (meaning we havent selected a referenced seller) then just set 
           * the lat/long to the city's lat/long 
           */
          if (pobox_locator_selected_id == null || pobox_seller_lat == null || pobox_seller_long == null) {

              if (!pobox_disable_locator_map_during_post) {
                   if (!pobox_bypass_location) {
                       pobox_seller_locate(1); 
                       return;
                   }
              }
              
              pobox_seller_lat = results[0].geometry.location.lat();
              pobox_seller_long = results[0].geometry.location.lng();

          }

          if (!pobox_bypass_spam) {
              if (pobox_spamfilter()) {
                  return;
              }
          }

         if (!pobox_bypass_misc) {
              if (pobox_misc_warn()) {
                  return;
              }
         }
    
         pobox_post();
    
      } else {
              
          if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
              m.push("<strong>Location</strong> is invalid");
              pobox_show_error_box(m);
          }
      }
    });


    // })
    //.fail( function () { 
    
    //    m.push("Server Error (geolocation)");
    //    pobox_show_error_box(m);
    //    return;    
    
    //});  


}

function pobox_post() {

    //$(window).resize();
 
    pobox_suppress_warning_box_hide = false;

 
    text = $("#pobox_post_button_text");
    text.html("<img src='images/loader3.gif'>");

    pobox_save_context();
  
    $.post("php/post.php?captcha=new", 

       pobox_filter_context(pobox_context),

       function(data) { pobox_post_process(data); },

        
       "json"

    ).error(function(data) { pobox_post_error(["Server Error"]); });

    pobox_bypass_spam = false;
    pobox_bypass_location = false;
    pobox_bypass_misc = false;
}



pobox_context = {};


function pobox_clear_context() {

    
    pobox_context = {};
    

}


function pobox_save_context() {

      pobox_context = {

 
           item_image_index: pobox_item_image_index,
           seller_image_index: pobox_seller_image_index,
           item_image_box_state: pobox_item_image_box_state,
           seller_image_box_state: pobox_seller_image_box_state,



           item  : $("#pobox_item_name_input").attr("value"),  
           value : parseInt($("#pobox_item_value_input").attr("value")),
           seller: $("#pobox_seller_input").attr("value"), 

           category   : $("#pobox_category_input").attr("value"),
           link       : $("#pobox_link_input").attr("value"),
           description: $("#pobox_description_input").attr("value"),


           item_image  : pobox_item_images[pobox_item_image_index],
           seller_image: pobox_seller_images[pobox_seller_image_index],


           online_seller: pobox_seller_online,

           seller_name     : pobox_locator_selected_name,
           seller_location : $("#pobox_location_input").attr("value"),
           seller_lat      : pobox_seller_lat,
           seller_long     : pobox_seller_long,
           seller_ref      : pobox_locator_selected_ref,
           seller_id       : pobox_locator_selected_id,


           latitude : pobox_seller_online ? user_latitude : pobox_seller_lat,
           longitude: pobox_seller_online ? user_longitude: pobox_seller_long,
           location : pobox_seller_online ? user_location : $("#pobox_location_input").attr("value"),


           flagged: pobox_bypass_spam

      };

 
  
 
}

function pobox_filter_context(c) {

    cc = c;
    return cc;

}


function pobox_restore_context() {

    $("#pobox_item_name_input" ).attr("class", "pobox_input");
    $("#pobox_item_value_input").attr("class", "pobox_cost_input");
    $("#pobox_seller_input").attr("class", "pobox_seller_input");
    $("#pobox_location_input").attr("class", "pobox_seller_input");

    $("#pobox_item_name_input").attr("value", pobox_context.item);
    $("#pobox_item_value_input").attr("value", pobox_context.value);
    $("#pobox_seller_input").attr("value", pobox_context.seller);
    $("#pobox_location_input").attr("value", pobox_context.location);
    $("#pobox_catetory_input").attr("value", pobox_context.category);
    $("#pobox_link_input").attr("value", pobox_context.link);
    $("#pobox_description_input").attr("value", pobox_context.description);  


    //pobox_item_images = pobox_context.item_images;
    pobox_item_image_index = pobox_context.item_image_index;
    pobox_item_image_box_state = pobox_context.item_image_box_state;
 
    //pobox_seller_images = pobox_context.seller_images;
    pobox_seller_image_index = pobox_context.seller_image_index;
    pobox_seller_image_box_state = pobox_context.seller_image_box_state;

    pobox_set_item_image(pobox_item_images[pobox_item_image_index]);
    pobox_set_seller_image(pobox_seller_images[pobox_seller_image_index]);

    pobox_seller_online = pobox_context.online_seller;

    pobox_locator_selected_name = pobox_context.seller_name;
    pobox_locator_selected_location = pobox_context.seller_location;
    pobox_locator_selected_id = pobox_context.seller_id;
    pobox_locator_selected_ref = pobox_context.seller_ref;

    pobox_seller_lat = pobox_context.seller_lat;
    pobox_seller_long = pobox_context.seller_long;

    if (pobox_seller_online) pobox_set_online_seller();

 
    if (pobox_locator_selected_id != null) pobox_set_seller_ref(pobox_locator_selected_name,
                                                                pobox_locator_selected_location,
                                                                pobox_locator_selected_ref,
                                                                pobox_locator_selected_id, 
                                                                0);

}


function pobox_init_spamfilter() {

    $.ajax({url: "php/spamwords.php" , dataType: "json"}).done(function(spam) {

        spamwords = spam;
 
    });
}



function pobox_init(first) {

    if (first) {
    pobox_seller_image_first = 0;
    pobox_item_image_first = 0;

    
    pobox_item_initial = 1;
    pobox_seller_initial = 1;
    pobox_location_initial = 1;
    } else {
    pobox_seller_image_first = 0;
    pobox_item_image_first = 0;

    
    pobox_item_initial = 0;
    pobox_seller_initial = 0;
    pobox_location_initial = 0;
    }
    
    
    pobox_item_name = "";
    
    pobox_item_value = 0;
    pobox_seller_name = "";

    pobox_seller_online = 0;
    pobox_location = "";
    pobox_location_detail = "";
    
    
    if (first) { 
    
    pobox_item_images = null;
    pobox_item_image_index = 0;
    pobox_item_name_for_image_update = "";
    
    pobox_seller_images = null;
    pobox_seller_image_index = 0;
    pobox_seller_name_for_image_update = "";

    }

 
    pobox_seller_lat = null;
    pobox_seller_long = null;
    
    
 
    
    pobox_item_image_box_state = IMBOX_INITIAL;
    pobox_seller_image_box_state = IMBOX_INITIAL;

    pobox_locator_map = null;
    pobox_geocoder = new google.maps.Geocoder();
    pobox_marker = [];
    pobox_locator_center = null;
    
    
    pobox_locator_results = null;
    pobox_locator_page = -1;
    pobox_locator_mouseout = 0;
    
    
    pobox_locator_selected_name = null;
    pobox_locator_selected_location = null;
    pobox_locator_selected_id = null;
    pobox_locator_selected_ref = null;

    pobox_map_shown = 0;





    
    if (first) pobox_fill_random();  

    $("#pobox_item_name_input").focusin(function() {
        pobox_hide_error_box(0);
        if (pobox_item_initial) {
            $("#pobox_item_name_input" ).attr("class", "pobox_input").attr("value", "");
            $("#pobox_item_value_input").attr("class", "pobox_cost_input").attr("value", "");
            pobox_item_initial = 0;
        }
    }).click(function() {pobox_hide_error_box(0); });
    
    $("#pobox_item_value_input").focusin(function() {
        pobox_hide_error_box(0);
        if (pobox_item_initial) {
            $("#pobox_item_name_input ").attr("class", "pobox_input").attr("value", "");
            $("#pobox_item_value_input").attr("class", "pobox_cost_input").attr("value", "");
            pobox_item_initial = 0;
        }
    }).click(function() {pobox_hide_error_box(0); });                                        
    
    $("#pobox_seller_input").focusin(function() {
        pobox_hide_error_box(0);
        if (pobox_seller_initial) {
            $("#pobox_seller_input").attr("class", "pobox_seller_input").attr("value", "");
            pobox_seller_initial = 0;
        }
    }).click(function() {pobox_hide_error_box(0); });                                           
    
    $("#pobox_location_input").focusin(function() {
        pobox_hide_error_box(0);
        if (pobox_location_initial) {
            $("#pobox_location_input").attr("class", "pobox_seller_input").attr("value", "");
            pobox_location_initial = 0;
        }
    }).click(function() {pobox_hide_error_box(0); });
    
    
    $("#pobox_item_name_input").typing({stop: pobox_item_image_update, delay: 1000 });
    $("#pobox_item_name_input").typing({stop: pobox_item_name_suggest, delay: 0 });
    $("#pobox_item_name_input").keydown( pobox_navigate_item_name_suggestions );

    $("#pobox_seller_input").typing({ stop: pobox_seller_image_update , delay: 1000 });
    $("#pobox_seller_input").typing({ stop: pobox_seller_name_suggest, delay: 0 });
    $("#pobox_seller_input").typing({ stop: pobox_seller_name_locator_changed, delay: 0 });
    $("#pobox_seller_input").keydown( pobox_navigate_seller_suggestions );

    $("#pobox_location_input").typing({ stop: pobox_location_update, delay: 0  });
    $("#pobox_location_input").typing({ stop: pobox_location_locator_changed, delay: 0  });
    $("#pobox_location_input").keydown( pobox_navigate_places_suggestions );
    
    
    pobox_image_ext_init(); 

    pobox_pager_init();

    pobox_item_image_box_state = IMBOX_INITIAL;
    pobox_seller_image_box_state = IMBOX_INITIAL;

    if (first) {
        pobox_item_image_update();
        pobox_seller_image_update();
    }

    $("#pobox_post_button").click(function() {  pobox_verify_and_post();  });
}




pobox_item_initial = 1;
pobox_seller_initial = 1;
pobox_location_initial = 1;


pobox_item_name = "";
pobox_item_name_for_image_update = "";
pobox_item_value = 0;
pobox_seller_name = "";
pobox_seller_name_for_image_update = ""; // secondary seller name is kept
pobox_seller_online = 0;
pobox_location = "";
pobox_location_detail = "";


pobox_seller_lat = null;
pobox_seller_long = null;





//----------------------------------------------------------------------
// POBOX MAP LOCATOR
//----------------------------------------------------------------------

 
pobox_locator_menu_processing = 0;
pobox_suppress_locator_show = 0;
pobox_map_shown = 0;

function pobox_locator_menu_hide() {
    
    
   
    footer = $("#pobox_locator_footer");
    menu = $("#pobox_locator_menu");

    //foo = $("#pobox_locator_menu_footer");
    //foo.html('');
    
    footer.hide();
    if (pobox_locator_menu_processing) {pobox_suppress_locator_show = 1; }

    menu.hide();


    pobox_map_shown = 0;
    
}

 
function pobox_locator_menu_show(fast) {

    cont = $("#main_container");
    footer = $("#pobox_locator_footer");
    menu = $("#pobox_locator_menu");
    button = null; $("#pobox_seller_locator");
    pobox = $("#pobox_inputs"); 
    
    if (pobox_show_locator_map_during_post == false) {
        button =  $("#pobox_seller_locator");
    } else {
        button =  $("#pobox_post_button");
    }

    menu.css("position", "absolute");
    menu.css({left: cont.offset().left +15, top: button.offset().top - 10, width: 420});
    if (fast)  menu.show() ; else  menu.fadeIn(100);
    menu.css({top: button.offset().top - 10 - menu.height()}); 
     
    footer.css("position", "absolute");
    footer.css({left: button.offset().left, top: menu.offset().top + menu.height()+1, width: button.width(), height: 10});
    if (fast) footer.show() ; else footer.fadeIn(100);

    pobox_map_shown = 1;

    pobox_adjust_locator_map();

    pobox_hide_error_box(1);

}


pobox_locator_map = null;
pobox_geocoder = new google.maps.Geocoder();
pobox_marker = [];
pobox_locator_center = null;

pobox_locator_results = null;
pobox_locator_page = -1;
pobox_locator_mouseout = 0;


pobox_locator_selected_name = null;
pobox_locator_selected_location = null;
pobox_locator_selected_ref = null;
pobox_locator_selected_id = null;



function pobox_clear_locator_results() {

    h = $("#pobox_locator_menu_header");
    h.html("<img src='images/loader.gif'>");

    tab = $("#pobox_locator_results"); tab.html('');
    foo = $("#pobox_locator_menu_footer"); foo.html('');


}

function pobox_clear_locator_map() {

    for (i = 1 ; i <= 20; i++) {         
        if (pobox_marker[i] != null) {
            pobox_marker[i].setVisible(false);
        }
    }

    if (pobox_locator_map != null) pobox_locator_map.setCenter(pobox_locator_center); 
}



/*
 * Todo: this is gona be a bitch function!
 */
function pobox_vicinity_get_city(v) {
    p = 0;
    c = "";
    L = 0;

    commas = 0;

    while ((p = v.indexOf(",", p+1)) > 0) {
        commas++;
    }

    if (commas <= 1) {
        return $.trim(v);
    }

    if ((p = v.indexOf(",", p+1)) > 0) {
        L = p;
        return $.trim((v.substring(L+1)));
    } else {
        return $.trim(v);
    }
}





function pobox_process_locator_vicinity_results(r) {

    if (r == null || r.length == 0) {

        city = pobox_vicinity_get_city(pobox_locator_selected_location);
        $("#pobox_location_input").attr('value', city);
        pobox_locator_selected_location = city;
        
    } else {
        city = $.trim(pobox_vicinity_get_city(r[0]));
        $("#pobox_location_input").attr('value', city);
        pobox_locator_selected_location = city;
    }
}

function pobox_set_seller_ref(N, V, R, D, u) {

    pobox_locator_selected_name = N;
    pobox_locator_selected_location = V;
    pobox_locator_selected_ref = R;
    pobox_locator_selected_id = D;


    // set the seller name to the exact name we recv'd from google
    $("#pobox_seller_input").attr('value', N); 
    if (pobox_suggested_seller_name == null) pobox_seller_image_update();
 
    if (u) {
        // set the location to (city of places search for vicinity)
        $.ajax({url: "php/places.php?&search=" + escape(V) , dataType: "json"})
        .done(  pobox_process_locator_vicinity_results  )
        .fail(   ); 
    } else {

        pobox_locator_selected_location = $("#pobox_location_input").attr('value'); 

    }



    $("#pobox_seller_locator_image").attr('src', 'images/map-set.png');
    //$("#pobox_seller_locator_image").attr('alt', p.results[i].name+' - '+p.results[i].vicinity); // XXX: why does this cause error??

}

function pobox_reset_seller_ref() {

    pobox_locator_selected_name = "";
    pobox_locator_selected_location = "";
    pobox_locator_selected_ref = null;
    pobox_locator_selected_id = null;

    pobox_seller_lat = null;
    pobox_seller_long = null;
        
    $("#pobox_seller_locator").html("<img  id='pobox_seller_locator_image' src='images/location.png' style='cursor:pointer' onclick='pobox_seller_locate(0);'>");

}


function pobox_seller_name_locator_changed() {

    v = $("#pobox_seller_input").attr('value');

    if (strnew(pobox_locator_selected_name, v)) {

        pobox_reset_seller_ref();

    }
}

function pobox_location_locator_changed() {

    v = $("#pobox_location_input").attr('value');

    if (strnew(pobox_locator_selected_location, v)) {

        pobox_reset_seller_ref();

    }
}



function pobox_set_locator_suggestion(m) {
    i = m.xxxx - 1;
    p = pobox_locator_results;

    N = p.results[i].name;

    if (pobox_suggested_seller_name != null) {
        nn = $.trim(N.replace("'", "")).toLowerCase();
        vv = pobox_suggested_seller_name;
        tt = $.trim(vv.replace("'", "")).toLowerCase();

        if (nn.indexOf(tt) >= 0) {
            //TODO: N = titlecase(vv);
        }
    }
    
     

    V = p.results[i].vicinity;
    R = p.results[i].reference;
    D = p.results[i].id;

    pobox_seller_lat = p.results[i].geometry.location.lat;
    pobox_seller_long = p.results[i].geometry.location.lng;
 
    pobox_set_seller_ref(N, V, R, D, 1);

    pobox_locator_menu_hide();

    
}





function pobox_hilite_locator_result(i, repos) {
     m = i ;
     l = i-1;
     pobox_marker[m].setZIndex(999999);
     pobox_marker[m].setIcon(new google.maps.MarkerImage('images/map/map-hilite-'+m+'.png'));
     $('#pobox_locator_result_'+l).css('background-color', '#EEEEEE'); 
     if (repos) pobox_locator_map.panTo(pobox_marker[m].getPosition());
     pobox_locator_mouseout = 0;
}

function pobox_unhilite_locator_result(i, repos) {
     m = i ;
     l = i-1;
     pobox_marker[m].setZIndex(pobox_marker[m].zzzz);
     pobox_marker[m].setIcon(new google.maps.MarkerImage('images/map/map-'+m+'.png'));
     $('#pobox_locator_result_'+l).css('background-color', '#FFFFFF'); 
     pobox_locator_mouseout = 1;
     if (repos) setTimeout("if (pobox_locator_mouseout) pobox_locator_map.panTo(pobox_locator_center);", 500);
}

function pobox_none_of_these() {
    pobox_locator_menu_hide();
    pobox_reset_seller_ref();
}

function pobox_disable_pager_during_post() {

    pobox_locator_menu_hide();

    pobox_disable_locator_map_during_post = 1;

    session_save_variables();


}


function pobox_enable_pager_during_post() {

    pobox_locator_menu_hide();


    pobox_disable_locator_map_during_post = 0;

    session_save_variables();
}

function pobox_pager_button_reset() {


    if (pobox_locator_selected_id != null) {


        $("#pobox_seller_locator").html("<img  id='pobox_seller_locator_image' src='images/map-set.png' style='cursor:pointer' onclick='pobox_seller_locate(0);'>");
    } else {
        $("#pobox_seller_locator").html("<img  id='pobox_seller_locator_image' src='images/location.png' style='cursor:pointer' onclick='pobox_seller_locate(0);'>");
    }
}

pobox_suppress_warning_box_hide = false;


function pobox_fill_pager_footer(p,s, last) {

    foo = $("#pobox_locator_menu_footer");

    if (!last) {
    
        foo.html('');
        nav = "";
        if (p.results.length > 4) {
            nav ="<nobr><img id='pobox_locator_scroll_left' src='images/leftarrowsmall.png' style='cursor:pointer'     onclick='pobox_fill_pager_results("+ (s-4) +", 0);' >\
                 <img id='pobox_locator_scroll_right' src='images/rightarrowsmall.png'  style='cursor:pointer'  onclick='pobox_fill_pager_results("+ (s+4) +", 0);'  >&nbsp;</nobr>";
        }
        opts = "";
        if (pobox_show_locator_map_during_post) {
            opts += "&middot; <a class='pobox_pager_control' style='cursor:pointer' onclick='pobox_disable_pager_during_post()'> Don't show anymore </a>"; 
        } else {
            if (pobox_disable_locator_map_during_post) {
                 opts += "&middot; <a class='pobox_pager_control' style='cursor:pointer' onclick='pobox_enable_pager_during_post()'> Show before posting </a>"; 
            }
        }
        row = "<tr id='pobox_locator_listing_row'><td  width='1%'   valign='middle'> "+nav+"\
                 </td><td valign='middle' id='pobox_locator_listing_text'><nobr>Listing "+(s+1)+"-"+((s+4) > p.results.length ? p.results.length : (s+4))+" of "+p.results.length+" results</nobr>\
                 </td><td><div align='right'>  <a class='pobox_pager_control' style='cursor:pointer' onclick='pobox_none_of_these();' > None of these </span> " + opts + " </div></td></tr>";
        foo.html(row);

    } else {

        foo.html('');
        nav = "";
        opts = "";
        if (pobox_show_locator_map_during_post) {
            opts += "<a class='pobox_pager_control' style='cursor:pointer' onclick='pobox_disable_pager_during_post()'> Don't show anymore </a>"; 
        } else {
            if (pobox_disable_locator_map_during_post) {
                 opts += "<a class='pobox_pager_control' style='cursor:pointer' onclick='pobox_enable_pager_during_post()'> Show before posting </a>"; 
            }
        }
        row = $("<tr id='pobox_locator_listing_row'><td  width='1%'   valign='middle'> "+nav+"\
                 </td><td valign='middle' id='pobox_locator_listing_text'><nobr></nobr>\
                 </td><td><div align='right'> " + opts + " </div></td></tr>");
        foo.append(row);

    }

    

}

function pobox_fill_pager_results(s, initial) { 

    p = pobox_locator_results;

    if (p.results.length == 0) { 

        sname = $("#pobox_seller_input").attr("value");
        lname = $("#pobox_location_input").attr("value");

        if (pobox_show_locator_map_during_post) {

            pobox_suppress_warning_box_hide = true;

            pobox_pager_button_reset(); 

            pobox_show_warning_box(["Could not find <strong>"+sname+"</strong>",
                                    "around <strong>"+lname.substring(0, 25)+"</strong>","",
                                    "<span onclick='pobox_bypass_location = true; pobox_verify_and_post();' style='font-size:7pt; ; cursor: pointer; color:#999999;'>Post Anyway</span>",
                                    "<span onclick='pobox_disable_pager_during_post(); pobox_hide_error_box(1);' style='font-size:7pt; ; cursor: pointer; color:#999999;'>Don't show this anymore</span>"]);

            return;
        }
    }

    if (s < 0) s = 0;
    if (s > p.results.length-1) {

        if (!pobox_suppress_locator_show) {
            pobox_locator_menu_show(1);
            setTimeout('pobox_locator_menu_show(1);', 20);

            pobox_pager_button_reset();

        }

        google.maps.event.trigger(pobox_locator_map, 'resize');
        pobox_locator_map.setCenter(pobox_locator_center); 

        if (p.results.length == 0) pobox_fill_pager_footer(p,s, 1); 

        return; 
    }

    pobox_locator_map.setCenter(pobox_locator_center); 

    tab = $("#pobox_locator_results"); tab.html('');

    for (i = s; i < s + 4 && i < p.results.length; i++) {  
        row = $("<tr style='cursor:pointer' id='pobox_locator_result_"+i+"' \
                onmouseover=\" pobox_hilite_locator_result("+(i+1)+", 1) ;\" \
                onmouseout=\"pobox_unhilite_locator_result("+(i+1)+", 1) ;\"\
                onclick=\"pobox_set_locator_suggestion(pobox_marker["+(i+1)+"]); \"   > \
                <td width='1%' valign='middle'><div align='left'><img src='images/map/map-"+(i+1)+".png'></div></td> \
                <td valign='middle'>"+ p.results[i].name +" - "+  p.results[i].vicinity +"</td></tr>");
        tab.append(row);
    }

    pobox_fill_pager_footer(p,s, 0);

    
    if ($('#pobox_locator_hspacer').attr('width') == undefined || $('#pobox_locator_hspacer').attr('width') <  $('#pobox_locator_listing_text').width()) {
        $('#pobox_locator_hspacer').attr('width', $('#pobox_locator_listing_text').width() + 4);
    }
     
 
    pobox_locator_menu_show(!initial);


    pobox_pager_button_reset();


    for (k = 10; k < 400; k+=50) {
        setTimeout("pobox_locator_menu_show(1)", k); 
    }
   

}

function pobox_process_pager_data_fail() {

    pobox_show_error_box(["Server Error"]);

    pobox_pager_button_reset();


    pobox_locator_menu_processing = 0;


}

function pobox_process_pager_data(p)  { 

    if (p.results.length == 0) {
        pobox_clear_locator_map();
        //return;
    }
 
    se = $("#pobox_seller_input");
    lo = $("#pobox_location_input");

    seller = $.trim(se.attr("value"));
    loc = $.trim(lo.attr("value"));


    /*
     * TODO -----
     */
    for (i = 0 ; i < p.results.length; i++) {  

        N = p.results[i].name
        V = p.results[i].vicinity;
        S = pobox_suggested_seller_name;

        if (/*S == null*/ 1) {
            //L = strlcs(N, V);  

            //if (L.length > 5) {
 
            //    if ($.trim(seller).toLowerCase() != $.trim(L).toLowerCase()) {
            //        idx = N.indexOf(L)
            //        N = N.substring(0, idx-1); 
            //        N = N.replace(L, " "); 
            //    }
            //}

            x = N.indexOf("-"); 

            if (x > 3) {
                N = N.substring(0, x-1);
            }

            N = N.replace("of", " ");
            N = N.replace("-"," ");

            p.results[i].name = N;
            //TODO - filter out junk such as '-', 'comma', 'of', etc.
        } else {
            nn = N.replace("'", "");
            ss = S.replace("'", "");
            L = strlcs($.trim(nn.toLowerCase()), $.trim(ss.toLowerCase()));
            if (L.length > 4) {
               //p.results[i].name = titlecase(L);
            }
        }
    }

    pobox_locator_results = p;

    pobox_locator_menu_processing = 0;



    h = $("#pobox_locator_menu_header");

    if (pobox_show_locator_map_during_post) {
         h.html("<table class='pobox_text2'><tr><td> <span style='color:#ff0000; font-weight: bold'>Choose one</span> </td> <td> of <strong>" + p.results.length + "</strong> results for <strong>" + seller + "</strong> around <strong>"+ loc +"</strong> </td></tr></table>");
        //h.html("<span style='color:#ff0000; font-weight: bold'>Choose one</span> of " + "<strong>" + p.results.length + "</strong> results for <strong>" + seller + "</strong> around <strong>"+ loc +"</strong>");
    } else {
        h.html("Found " + "<strong>" + p.results.length + "</strong> results for <strong>" + seller + "</strong> around <strong>"+ loc +"</strong>");
    }

    // initialize markers

    foo = $("#pobox_locator_menu_footer");
    foo.html('');

   if (!pobox_suppress_locator_show) pobox_fill_pager_results(0, 1); 

   pobox_suppress_locator_show = 0;

}


function pobox_marker_mouseover(m){
     m.setZIndex(999999);
     m.setIcon(new google.maps.MarkerImage('images/map/map-hilite-'+m.xxxx+'.png'));
     pobox_hilite_locator_result(m.xxxx, 0);
}

function pobox_marker_mouseout(m){
     m.setZIndex(m.zzzz);
     m.setIcon(new google.maps.MarkerImage('images/map/map-'+m.xxxx+'.png'));
     pobox_unhilite_locator_result(m.xxxx, 0);
}

function pobox_marker_click(m){
      
     pobox_set_locator_suggestion(m);
}

function pobox_marker_add_listeners(m) {

    google.maps.event.addListener(m, 'mouseover', function() {
        pobox_marker_mouseover(m);
    });


    google.maps.event.addListener(m, 'mouseout', function() {
        pobox_marker_mouseout(m);
    });

    google.maps.event.addListener(m, 'click', function() {
        pobox_marker_click(m);
    });

}



function pobox_pager_init() {

    pobox_locator_center = new google.maps.LatLng(36, -119);

    var options = {
        zoom: 10,
        center: pobox_locator_center,
        streetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };


    pobox_locator_map = new google.maps.Map(document.getElementById("pobox_locator_map_container"), options);

    for (i = 1 ; i <= 20; i++) {         
        pobox_marker[i] = new google.maps.Marker({
            map: pobox_locator_map,
            icon: "images/map/map-"+i+".png",
            position: pobox_locator_center,
            visible: false
        });

        pobox_marker[i].xxxx=i;
        pobox_marker[i].zzzz = 999999-i;

        pobox_marker_add_listeners(pobox_marker[i]);

    }
}



 
function pobox_adjust_locator_map() {
 
    google.maps.event.trigger(pobox_locator_map, 'resize');

    pobox_locator_map.setCenter(pobox_locator_center);  
 
    for (i = 1; i < 20; i++) {
        pobox_marker[i+1].setVisible(false);
    }

    for (i = 0; i < p.results.length; i++) {
        pobox_marker[i+1].setPosition(new google.maps.LatLng(p.results[i].geometry.location.lat, p.results[i].geometry.location.lng));
        pobox_marker[i+1].setVisible(true);
        pobox_marker[i+1].setTitle(p.results[i].name + ' - ' + p.results[i].vicinity);
        pobox_marker[i+1].setZIndex(pobox_marker[i+1].zzzz);
    }
 
}


pobox_show_locator_map_during_post = 0;
pobox_disable_locator_map_during_post = 0;

function pobox_request_pager_data(addr, seller, posting) {

     if (pobox_map_shown) return;

     pobox_clear_locator_results();

     pobox_show_locator_map_during_post = posting;

     if (!pobox_show_locator_map_during_post) {
         $("#pobox_seller_locator").html("<img  id='pobox_seller_locator_image' src='images/loader2.gif' height='13' '>");
     }
  

     pobox_geocoder.geocode( { 'address': addr}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {

          var latlng = results[0].geometry.location;  
          pobox_locator_center = latlng;
    

          tab = $("#pobox_locator_results"); tab.html('');
          foo = $("#pobox_locator_menu_footer"); foo.html('');
           
          if (!pobox_locator_menu_processing)  {

              $.ajax({url: "php/searchplaces.php?search=" + escape(seller) + "&address=" + escape(addr) + "&location=" + (latlng.toUrlValue()), dataType: "json"})
              .done(  pobox_process_pager_data  )
              .fail(  pobox_process_pager_data_fail  );
    
              pobox_locator_menu_processing = 1;

          }

 
      } else {
          
          if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {

              m = [];
              m.push("Could not find <strong>"+addr+"</strong>");
              pobox_show_warning_box(m);
              
              pobox_pager_button_reset();
          }

      }
    });
}


function pobox_seller_locate(posting) 
{
    se = $("#pobox_seller_input");
    lo = $("#pobox_location_input");

    seller = se.attr("value");
    loc = lo.attr("value");

    m = [];

    err = 0;

    if ($.trim(seller) == "") {
        err = 1;
        m.push("<strong>From</strong> box is empty");
    }

    if ($.trim(loc) == "") {
        err = 1;
        m.push("<strong>Location</strong> box is empty");
    }

    if (err) {
        pobox_show_error_box(m);
        return;
    }

 
    pobox_request_pager_data($.trim(loc), $.trim(seller), posting);
 
    
}



























pobox_item_images = null;
pobox_item_image_index = 0;

pobox_seller_images = null;
pobox_seller_image_index = 0;


IMBOX_INITIAL = 0;
IMBOX_NORMAL = 1;
IMBOX_LOCKED = 2;
IMBOX_STIFF = 3;
IMBOX_REMOVED = 4;

pobox_item_image_box_state = IMBOX_INITIAL;
pobox_seller_image_box_state = IMBOX_INITIAL;


function pobox_item_image_update() {
    
    val = $("#pobox_item_name_input").attr("value");

    if (pobox_item_image_box_state == IMBOX_LOCKED) return;
    //if (pobox_item_image_box_state == IMBOX_REMOVED) return;
 
    if ($.trim(val) == "") {
        $("#pobox_item_image").html("<img src='images/none.png'>"); 
        pobox_item_images = ['images/none.png'];
        pobox_item_image_index = 0;
        pobox_item_image_box_state = IMBOX_INITIAL;
        pobox_item_name_for_image_update = "";
        return;
    }
    
    if (strnew(pobox_item_name_for_image_update, val)) {
        pobox_search_item_images(val);
        
        if (pobox_item_image_box_state == IMBOX_INITIAL) pobox_item_image_box_state = IMBOX_NORMAL;
        
    }

    if (pobox_item_image_box_state == IMBOX_REMOVED) {
        pobox_search_item_images(val);
        pobox_item_image_box_state = IMBOX_NORMAL;
        pobox_item_image_box_controls_render(); 
    }
    
    if (val != undefined && val != "") pobox_item_name_for_image_update = val;
}



function pobox_seller_image_update() { 

    val = $("#pobox_seller_input").attr("value");  


    if (pobox_seller_image_box_state == IMBOX_LOCKED) return;
    //if (pobox_seller_image_box_state == IMBOX_REMOVED) return;

    if ($.trim(val) == "") {
        $("#pobox_seller_image").html("<img src='images/none.png'>"); 
        pobox_seller_images = ['images/none.png'];
        pobox_seller_image_index = 0;
        pobox_seller_image_box_state = IMBOX_INITIAL;
        pobox_seller_name_for_image_update = "";
        return;
    }
    
    if (strnew(pobox_seller_name_for_image_update, val)) {
        pobox_search_seller_images(val);
        
        if (pobox_seller_image_box_state == IMBOX_INITIAL) pobox_seller_image_box_state = IMBOX_NORMAL;
    
    }

    if (pobox_seller_image_box_state == IMBOX_REMOVED) {
        pobox_search_seller_images(val);
        pobox_seller_image_box_state = IMBOX_NORMAL;
        pobox_seller_image_box_controls_render();
    }

    if (val != undefined  && val != "") pobox_seller_name_for_image_update = val;
    
}

function pobox_item_image_remove() {
    $("#pobox_item_image").html("<img src='images/no.png'>"); 
    pobox_item_images = ['images/no.png'];
    pobox_item_image_index = 0;
}

function pobox_seller_image_remove() {
    $("#pobox_seller_image").html("<img src='images/no.png'>"); 
    pobox_seller_images = ['images/no.png'];
    pobox_seller_image_index = 0;
}


function pobox_seller_name_suggest() { 

    val = $("#pobox_seller_input").attr("value");
 
    if ($.trim(val) == "") {
        pobox_seller_suggest_hide();
    }

    if (strnew(pobox_seller_name, val)) {
        pobox_seller_suggest(val);
        pobox_suggested_seller_name = null;
        
    }
   
    pobox_seller_name = val;
 
}

pobox_seller_suggest_suppress = 0;
pobox_seller_suggest_pending = 0;

function pobox_seller_suggest(s) { 


    pobox_seller_suggest_pending++;  


    pobox_unset_online_seller();

    $.ajax({url: "php/sellerinfo.php?search=" + escape(s) , dataType: "json"})
    .done(  pobox_seller_search_success )
    .fail(  pobox_seller_search_failure );  

}


function pobox_seller_search_success(sellers) {


    pobox_seller_suggest_show(sellers);

    pobox_suggested_seller_name = null;
    
    pobox_seller_suggest_pending--; 

    if (pobox_seller_suggest_pending == 0) pobox_seller_suggest_suppress = 0;
    
}

function pobox_seller_search_failure() {

 
    pobox_seller_suggest_pending--;
}



function pobox_location_update() {

    if (pobox_places_suggest_suppress) {
        pobox_places_suggest_suppress = 0;
        return;
    }

    val = $("#pobox_location_input").attr("value");
 
    if ($.trim(val) == "") {
        pobox_places_suggest_hide();
    }

    if ($.trim(val).toLowerCase() == "online") {
        pobox_location = "online";
        pobox_set_online_seller();
        pobox_places_suggest_hide();
        return;
    }

    if ($.trim(val).length >= 3 && $.trim(val).length < 20 && strnew(pobox_location, val)) {
        pobox_location_suggest(val);
    }
   
    pobox_location = val;
}


function pobox_location_suggest(s) {

    $.ajax({url: "php/places.php?search=" + escape(s) , dataType: "json"})
    .done(  pobox_location_search_success )
    .fail(  pobox_location_search_failure );  

}


function pobox_location_search_success(places) {
    pobox_places_suggest_show(places);
}

function pobox_location_search_failure() {


}



function pobox_item_name_suggest() { 

    val = $("#pobox_item_name_input").attr("value");
 
    if ($.trim(val) == "") {
        pobox_item_name_suggest_hide();
    }

    if ($.trim(val).length >=5 && strnew(pobox_item_name, val)) {
        pobox_item_suggest(val);
    }
   
    pobox_seller_name = val;
}
 
function pobox_item_suggest(s) { 

    google_suggest_callback = pobox_item_search_success;

    google_suggest(s);

    return;


    $.ajax({url: "php/suggest.php?search=" + escape(s) , dataType: "json"})
    .done(  pobox_item_search_success )
    .fail(  pobox_item_search_failure );  

}


function pobox_item_search_success(items) {

    items = items.slice(0,4);

    if (pobox_item_name_suggest_suppress) {
        pobox_item_name_suggest_suppress = 0;
        return ;
    }

    pobox_item_name_suggest_show(items);
}

function pobox_item_search_failure() {


}



pobox_item_image_searched = null;
function pobox_search_item_images(s) {

    pobox_item_image_searched = s;

    $("#pobox_item_image").html("<img src='images/loader.gif'>");
    $.ajax({url: "php/images.php?search=" + escape(s) , dataType: "json"})
    .done(  pobox_item_images_search_success )
    .fail(  pobox_item_images_search_failure );  
    
}


pobox_seller_image_searched = null;
function pobox_search_seller_images(s) {

    pobox_seller_image_searched = s;

    $("#pobox_seller_image").html("<img src='images/loader.gif'>"); 
    $.ajax({url: "php/sellerimages.php?search=" + escape(s) , dataType: "json"})
    .done(  pobox_seller_images_search_success )
    .fail(  pobox_seller_images_search_failure );       
    
}

pobox_item_image_first = 0;
function pobox_item_images_search_success(images) {

    header = images[0];

    if ($.trim(pobox_item_image_searched) != $.trim(header)) {
        return;
    }

    if (images.length == 1) {
        $("#pobox_item_image").html("<img src='images/null.png'>"); 
        pobox_item_images = ['images/null.png'];
        pobox_item_image_index = 0; pobox_item_image_pager_adjust();
        pobox_item_image_box_state = IMBOX_INITIAL;
        return;
    }

    pobox_item_images = images.slice(1);
    preload(pobox_item_images);
    pobox_item_image_index = 0; pobox_item_image_pager_adjust();
    setTimeout("pobox_set_item_image(pobox_item_images[pobox_item_image_index])", pobox_item_image_first ? 0 : 0);
    
}

function pobox_item_images_search_failure(images) {
    
    //JSON.stringify(images);
}


pobox_seller_image_first = 0;
function pobox_seller_images_search_success(images) {

    header = images[0];

    if ($.trim(pobox_seller_image_searched) != $.trim(header)) {
        return;
    }

    if (images.length == 1) {
        $("#pobox_seller_image").html("<img src='images/null.png'>"); 
        pobox_seller_images = ['images/null.png'];
        pobox_seller_image_index = 0; pobox_seller_image_pager_adjust();
        pobox_seller_image_box_state = IMBOX_INITIAL;
        return;
    }


    pobox_seller_images = images.slice(1);
    preload(pobox_seller_images);
    pobox_seller_image_index = 0; pobox_seller_image_pager_adjust();
    setTimeout("pobox_set_seller_image(pobox_seller_images[pobox_seller_image_index])", pobox_seller_image_first ? 0 : 0);
}

function pobox_seller_images_search_failure(images) {
    
    //JSON.stringify(images);
}


function pobox_set_item_image(image) {
    // Stop the loader gif and load the actual image
    $("#pobox_item_image").html("<img src='" + image + "' height='70'  >");    
    pobox_item_image_box_lr_adjust();
    pobox_item_image_first = 1;
}


function pobox_set_seller_image(image) {
    // Stop the loader gif and load the actual image
    $("#pobox_seller_image").html("<img src='" + image + "' height='70'  >");  
    pobox_seller_image_box_lr_adjust();
    pobox_seller_image_first = 1;
}

function pobox_scroll_item_image(d) {

    if (pobox_item_images.length <= 1) {
        return;
    }
    
    pobox_item_image_index = (pobox_item_image_index + d) % pobox_item_images.length;
    if (pobox_item_image_index  < 0) pobox_item_image_index = pobox_item_images.length - 1;
    pobox_item_image_pager_adjust();
    setTimeout("pobox_set_item_image(pobox_item_images[pobox_item_image_index])", pobox_item_image_first ? 0 : 0);
}

function pobox_scroll_seller_image(d) {

    if (pobox_seller_images.length <= 1)  {
        return;
    }
    
    pobox_seller_image_index = (pobox_seller_image_index + d) % pobox_seller_images.length;
    if (pobox_seller_image_index  < 0) pobox_seller_image_index = pobox_seller_images.length - 1;;
    pobox_seller_image_pager_adjust();
    setTimeout("pobox_set_seller_image(pobox_seller_images[pobox_seller_image_index])", pobox_seller_image_first ? 0 : 0);
}



function pobox_image_button_hilite(t) {
    n = t.target.src;
    N = n.replace(".png", "_hilite.png");
    t.target.src = N;
}

function pobox_image_button_normal(t) {
    n = t.target.src;
    N = n.replace("_hilite.png", ".png");
    t.target.src = N;
}

// initialize the image controls and buttons
function pobox_image_ext_init() {
    //hilite/unhilite
    $("#pobox_item_image_upload").hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_item_image_lock"  ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_item_image_unlock").hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_item_image_close" ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_item_image_use"   ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    
    //hoverintent
    $("#pobox_item_image_box").hoverIntent({over: pobox_show_item_image_box_ext,  out: pobox_hide_item_image_box_ext, timeout: 200});
    
    //action
    $("#pobox_item_image_upload").click();
    $("#pobox_item_image_lock"  ).click(function(){ pobox_item_image_box_state = IMBOX_LOCKED; pobox_item_image_box_controls_render();});
    $("#pobox_item_image_unlock").click(function(){ pobox_item_image_box_state = IMBOX_NORMAL; pobox_item_image_box_controls_render(); pobox_item_image_update();});
    $("#pobox_item_image_close" ).click(function(){ pobox_item_image_box_state = IMBOX_REMOVED;pobox_item_image_box_controls_render(); pobox_item_image_remove();});
    $("#pobox_item_image_use"   ).click(function(){ /*pobox_item_image_box_state = IMBOX_NORMAL;*/ pobox_item_image_box_controls_render(); pobox_item_image_update();});
    
    
    //hilite/unhilite
    $("#pobox_seller_image_upload").hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_seller_image_lock"  ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_seller_image_unlock").hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_seller_image_close" ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    $("#pobox_seller_image_use"   ).hover( pobox_image_button_hilite, pobox_image_button_normal );
    
    
    //hoverintent
    $("#pobox_seller_image_box").hoverIntent({over: pobox_show_seller_image_box_ext,  out: pobox_hide_seller_image_box_ext, timeout: 200});
    
    //action
    $("#pobox_seller_image_upload").click();
    $("#pobox_seller_image_lock"  ).click(function(){ pobox_seller_image_box_state = IMBOX_LOCKED; pobox_seller_image_box_controls_render();});
    $("#pobox_seller_image_unlock").click(function(){ pobox_seller_image_box_state = IMBOX_NORMAL; pobox_seller_image_box_controls_render(); pobox_seller_image_update();});
    $("#pobox_seller_image_close" ).click(function(){ pobox_seller_image_box_state = IMBOX_REMOVED;pobox_seller_image_box_controls_render(); pobox_seller_image_remove();});
    $("#pobox_seller_image_use"   ).click(function(){ /*pobox_seller_image_box_state = IMBOX_NORMAL;*/ pobox_seller_image_box_controls_render(); pobox_seller_image_update();});
    
    
    
}



function pobox_show_item_image_box_ext() {
    if (!pobox_map_shown) pobox_item_image_box_controls_render();
}

function pobox_show_seller_image_box_ext() {
    if (!pobox_map_shown) pobox_seller_image_box_controls_render();
}



function pobox_hide_item_image_box_ext() {
    $("#pobox_item_image_upload").fadeOut(150);
    if (pobox_item_image_box_state != IMBOX_LOCKED)
    $("#pobox_item_image_locked").fadeOut(150);
    $("#pobox_item_image_lock"  ).fadeOut(150);
    $("#pobox_item_image_unlock").fadeOut(150);
    $("#pobox_item_image_close" ).fadeOut(150);
    $("#pobox_item_image_use"   ).fadeOut(150);
    
    $("#pobox_item_image_box_left"  ).fadeOut(150);
    $("#pobox_item_image_box_right" ).fadeOut(150);
    $("#pobox_item_image_pager"     ).fadeOut(150);
}

function pobox_hide_seller_image_box_ext() {
    $("#pobox_seller_image_upload").fadeOut(150);
    if (pobox_seller_image_box_state != IMBOX_LOCKED)
    $("#pobox_seller_image_locked").fadeOut(150);
    $("#pobox_seller_image_lock"  ).fadeOut(150);
    $("#pobox_seller_image_unlock").fadeOut(150);
    $("#pobox_seller_image_close" ).fadeOut(150);
    $("#pobox_seller_image_use"   ).fadeOut(150);
    
    $("#pobox_seller_image_box_left"  ).fadeOut(150);
    $("#pobox_seller_image_box_right" ).fadeOut(150);
    $("#pobox_seller_image_pager"     ).fadeOut(150);
}

function pobox_hide_item_image_box_ext2() {
    $("#pobox_item_image_upload").hide();
    if (pobox_item_image_box_state != IMBOX_LOCKED)
    $("#pobox_item_image_locked").hide();
    $("#pobox_item_image_lock"  ).hide();
    $("#pobox_item_image_unlock").hide();
    $("#pobox_item_image_close" ).hide();
    $("#pobox_item_image_use"   ).hide();
    
    $("#pobox_item_image_box_left"  ).hide();
    $("#pobox_item_image_box_right" ).hide();
    $("#pobox_item_image_pager"     ).hide();
}

function pobox_hide_seller_image_box_ext2() {
    $("#pobox_seller_image_upload").hide();
    if (pobox_seller_image_box_state != IMBOX_LOCKED)
    $("#pobox_seller_image_locked").hide();
    $("#pobox_seller_image_lock"  ).hide();
    $("#pobox_seller_image_unlock").hide();
    $("#pobox_seller_image_close" ).hide();
    $("#pobox_seller_image_use"   ).hide();
    
    $("#pobox_seller_image_box_left"  ).hide();
    $("#pobox_seller_image_box_right" ).hide();
    $("#pobox_seller_image_pager"     ).hide();
}

function pobox_item_image_pager_adjust() {
    if (pobox_item_image_box_state != IMBOX_NORMAL) return;
    s = "";
    for (i = 0 ; i < pobox_item_images.length; i++) {
        if (i == pobox_item_image_index) s+="<img src='images/images_pager_black.png' />";
        else s+="<img src='images/images_pager_grey.png' />";
    }
    $("#pobox_item_image_pager").html(s);
}

function pobox_seller_image_pager_adjust() {
    if (pobox_seller_image_box_state != IMBOX_NORMAL) return;
    s = "";
    for (i = 0 ; i < pobox_seller_images.length; i++) {
        if (i == pobox_seller_image_index) s+="<img src='images/images_pager_black.png' />";
        else s+="<img src='images/images_pager_grey.png' />";
    }
    $("#pobox_seller_image_pager").html(s);
}

// adjust the left/right buttons that scroll thru images
// set their position correctly according to the size of the image
function pobox_item_image_box_lr_adjust() {
    cont = $("#pobox_item_image_container");
    left = $("#pobox_item_image_box_left");
    rite = $("#pobox_item_image_box_right");
    cpos = cont.position();
    left.css("position", "absolute");
    left.css({left: cpos.left+3, top: cpos.top + 59});
    rite.css("position", "absolute");
    rite.css({left: cpos.left + 18, top: cpos.top + 59});
}

// adjust the left/right buttons that scroll thru images
// set their position correctly according to the size of the image
function pobox_seller_image_box_lr_adjust() {
    cont = $("#pobox_seller_image_container");
    left = $("#pobox_seller_image_box_left");
    rite = $("#pobox_seller_image_box_right");
    cpos = cont.position();
    left.css("position", "absolute");
    left.css({left: cpos.left+3, top: cpos.top + 59});
    rite.css("position", "absolute");
    rite.css({left: cpos.left + 18, top: cpos.top + 59});
}

function pobox_item_image_box_controls_render() {
    pobox_hide_item_image_box_ext2();
    if (pobox_item_image_box_state == IMBOX_NORMAL) {
        
        pobox_item_image_pager_adjust();
        pobox_item_image_box_lr_adjust();
        
        $("#pobox_item_image_box_left").fadeIn(100);
        $("#pobox_item_image_box_right").fadeIn(100);
        $("#pobox_item_image_pager").fadeIn(100);
        $("#pobox_item_image_upload").fadeIn(100);
        $("#pobox_item_image_lock"  ).fadeIn(100);
        $("#pobox_item_image_close" ).fadeIn(100);
        
    } else if (pobox_item_image_box_state == IMBOX_LOCKED) {
        
        $("#pobox_item_image_locked").fadeIn(100);
        $("#pobox_item_image_unlock").fadeIn(100);
        
    } else if (pobox_item_image_box_state == IMBOX_STIFF) {
        
        $("#pobox_item_image_upload").fadeIn(100);
        $("#pobox_item_image_close").fadeIn(100);
        
    } else if (pobox_item_image_box_state == IMBOX_REMOVED) {
        
        $("#pobox_item_image_use").fadeIn(100);
        
    } else if (pobox_item_image_box_state == IMBOX_INITIAL) {
        
    }
}


function pobox_seller_image_box_controls_render() {
    pobox_hide_seller_image_box_ext2();
    if (pobox_seller_image_box_state == IMBOX_NORMAL) {
        
        pobox_seller_image_pager_adjust();
        pobox_seller_image_box_lr_adjust();
        
        $("#pobox_seller_image_box_left").fadeIn(100);
        $("#pobox_seller_image_box_right").fadeIn(100);
        $("#pobox_seller_image_pager").fadeIn(100);
        $("#pobox_seller_image_upload").fadeIn(100);
        $("#pobox_seller_image_lock"  ).fadeIn(100);
        $("#pobox_seller_image_close" ).fadeIn(100);
        
    } else if (pobox_seller_image_box_state == IMBOX_LOCKED) {
        
        $("#pobox_seller_image_locked").fadeIn(100);
        $("#pobox_seller_image_unlock").fadeIn(100);
        
    } else if (pobox_seller_image_box_state == IMBOX_STIFF) {

        $("#pobox_seller_image_upload").fadeIn(100);
        $("#pobox_seller_image_close").fadeIn(100);

    } else if (pobox_seller_image_box_state == IMBOX_REMOVED) {
        
        $("#pobox_seller_image_use").fadeIn(100);
        
    } else if (pobox_seller_image_box_state == IMBOX_INITIAL) {
        
    }

}


function pobox_show_warning_box(m) {

    im1=$("#pobox_item_image_box");
    im2=$("#pobox_seller_image_box");

    //im1.hide(); im2.hide();

    pobox_error = $("#pobox_error_box");
    pobox_error.attr("class", "warningbox");
    
    tab = $("#pobox_error_table");
    tab.attr("class", "pobox_text");

    location_box = $("#pobox_location_input");
    topbox = $("#pobox_images_top_left_corner");
    toppos = topbox.position();
    locpos = location_box.position();
    mp = $("#pobox_post_marker");
    mpos = mp.position();

    tab.html('');

    for (i = 0; i < m.length; i++) {
        row = $("<tr><td valign='middle' width='3%'>&nbsp;</td>  <td>"+m[i]+"</td></tr>");
        tab.append(row);
    }
    
    pobox_error.css("position", "absolute");
    pobox_error.css({left: toppos.left-10, top: toppos.top});
    pobox_error.css({width: (mpos.left) - ( toppos.left-10) - 10, height: 98});
    pobox_error.show();

    pobox_hilite_error_rows(m);

    
}


function pobox_hilite_error_rows(m) {

    for (i = 0; i < m.length; i++) {
        error = m[i];  

        if (error.indexOf("Bought") >= 0) {
            $("#pobox_bought_tag").css("color", "#FF0000");
        }

        
        if (error.indexOf("Price") >= 0) {
            setTimeout("$('#pobox_item_value_input').css('background-image', 'url(images/dollar-red.png)');", 0);
            
        }


        if (error.indexOf("From") >= 0) {
            $("#pobox_from_tag").css("color", "#FF0000");
        }

        if (error.indexOf("Location") >= 0) {
            $("#pobox_location_tag").css("color", "#FF0000");
        }


        if (error.indexOf("Link") >= 0) {
            $("#pobox_link_tag").css("color", "#FF0000");
        }

        if (error.indexOf("Description") >= 0) {
            $("#pobox_description_tag").css("color", "#FF0000");
        }
    }
}


function pobox_unhilite_error_rows() {

    $("#pobox_bought_tag").css("color", "#666");
    $("#pobox_from_tag").css("color", "#666");
    $("#pobox_location_tag").css("color", "#666");
    $("#pobox_item_value_input").css("background-image", "url(images/dollar.png)");

    $("#pobox_link_tag").css("color", "#666");
    $("#pobox_description_tag").css("color", "#666");

}


function pobox_show_error_box(m) { 

    im1=$("#pobox_item_image_box");
    im2=$("#pobox_seller_image_box");

    //im1.hide(); im2.hide();

    pobox_error = $("#pobox_error_box");
    pobox_error.attr("class", "errorbox");
    
    tab = $("#pobox_error_table");
    tab.attr("class", "errorboxtext");

    location_box = $("#pobox_location_input");
    topbox = $("#pobox_images_top_left_corner");
    toppos = topbox.position();
    locpos = location_box.position();
    mp = $("#pobox_post_marker");
    mpos = mp.position();

    tab.html('');

    //$("#pobox_post_marker").html("<img src='images/blank.png' width='39' height='21' />");

    for (i = 0; i < m.length; i++) {
        row = $("<tr><td valign='middle' width='3%'>" + 
                (m[i].charAt(0) == "!" ? "&nbsp;</td>" : "<img src='images/red.png'></td>"  )  + 
                "<td>"+(m[i].charAt(0) == "!" ? m[i].substring(1) : m[i] )+"</td></tr>");
        tab.append(row);
    }
    
    pobox_error.css("position", "absolute");
    pobox_error.css({left: toppos.left-10, top: toppos.top});
    pobox_error.css({width: (mpos.left) - ( toppos.left-10) - 10, height: 100});
    pobox_error.show();

    pobox_hilite_error_rows(m);
    
}

function pobox_hide_error_box(fast) { 

    pobox_error = $("#pobox_error_box");
  
    if (fast) pobox_error.hide(); else pobox_error.fadeOut(100);

    im1=$("#pobox_item_image_box");
    im2=$("#pobox_seller_image_box");

    //im1.fadeIn(100); im2.fadeIn(100);

    //setTimeout("$('#pobox_post_marker').html('<img src=\"images/post.png\" width=\"39\" height=\"21\" />');", 80);
  
    pobox_bypass_spam = false;
    pobox_bypass_location = false;
    pobox_bypass_misc = false;

    pobox_unhilite_error_rows();

}


/* ---------------------------------------------------------------------
 * Friendfeed  
 * ---------------------------------------------------------------------
 */

function friend_feed_populate() {
    show_friend_feed_wait();
    
    $.ajax({url: "php/friendfeed.php", dataType: "json"})
    .done(  hide_friend_feed_wait )
    .fail(  hide_friend_feed_wait );
}

function show_friend_feed_wait(m) {
    $("#friendfeed_container").html("<div align='center'><img src='images/loader.gif'></div>");
}

function hide_friend_feed_wait(m) {
    s = '';
    $("#friendfeed_container").html("&nbsp;");
    
    alert(JSON.stringify(m));
    
}

function show_friend_feed_collapse_ctrl() {
    space = $("#main_container_space");
    space.html("<img id='friend_feed_collapse_ctrl' onclick='hide_friend_feed();' src='images/collapse.png' style='cursor:pointer'>");
}


function hide_friend_feed_collapse_ctrl() {
    space = $("#main_container_space");
    space.html(" ");
}

friend_feed_shown = 0;


main_cont_space = null;
friendfeed_cont = null;
friendfeed_head = null;
main_head_width = 0;


function hide_friend_feed() {
    
    main_cont_space = $("#main_container_space").detach();
    friendfeed_cont = $("#friendfeed_container").detach();
    $("#main_container").attr("colspan", "3");


    friendfeed_head = $("#friendfeed_header").detach();
    main_head_width = $("#main_header").outerWidth();
    $("#main_header").width(1000);
    
    $(window).resize();
    
    friend_feed_shown = 0;

     main_container_update_view();
}

function show_friend_feed() {
    
    $("#header_row").append(friendfeed_head);
    $("#main_header").width(600);
    
    $("#main_container").attr("colspan", "1");
    $("#main_container_row").append(main_cont_space);
    $("#main_container_row").append(friendfeed_cont);
    
    $("#main_container_space").html(" ");
    
    //friend_feed_populate();
    
    $(window).resize();
    
    friend_feed_shown = 1;

    main_container_update_view();
}









